Skip to content

Commit

Permalink
feat(Twitter): Add flags via deep links
Browse files Browse the repository at this point in the history
  • Loading branch information
swakwork committed Oct 8, 2024
1 parent e8aa4cc commit 2c8bd5e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static boolean create(Activity act) {
fragment = new SettingsFragment();
}else if (intent.getBooleanExtra(Settings.FEATURE_FLAGS.key, false)) {
fragment = new FeatureFlagsFragment();
fragment.setArguments(intent.getExtras());
}else if (intent.getBooleanExtra(Settings.PATCH_INFO.key, false)) {
fragment = new SettingsAboutFragment();
}else{
Expand Down Expand Up @@ -59,16 +60,22 @@ public static void startFragment(Activity act, Fragment fragment,boolean addToBa
transaction.commit();
}

public static void startActivity(String activity_name)throws Exception{
public static void startActivity(String activity_name, Bundle bundle)throws Exception{
Intent intent = new Intent(context, Class.forName("com.twitter.android.AuthorizeAppActivity"));
Bundle bundle = new Bundle();
bundle.putBoolean(activity_name, true);
bundle.putBoolean(EXTRA_PIKO, true);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

public static void startActivity(String activity_name)throws Exception{
Bundle bundle = new Bundle();
bundle.putBoolean(activity_name, true);
bundle.putBoolean(EXTRA_PIKO, true);
startActivity(activity_name,bundle);
}

public static void startSettingsActivity() throws Exception {
startActivity(EXTRA_PIKO_SETTINGS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

@SuppressWarnings("deprecation")
public class DeepLink {
private static final String bundleFlagNameKey = "flagName";
private static final String bundleFlagValueKey = "flagValue";

public static boolean deeplink(Activity act) {
try {
Expand All @@ -35,6 +37,16 @@ public static boolean deeplink(Activity act) {
return true;
}else if(mainPath.equals("addflags")){
key = Settings.FEATURE_FLAGS;
String bundleFlagName = deeplink.getQueryParameter("f");
if(bundleFlagName!=null){
boolean bundleFlagValue = deeplink.getBooleanQueryParameter("v",true);
Bundle bundle = new Bundle();
bundle.putString(bundleFlagNameKey, bundleFlagName);
bundle.putBoolean(bundleFlagValueKey, bundleFlagValue);
ActivityHook.startActivity(key.key,bundle);
return true;

}
}else if (isPiko) {
if(lastSegment.equals("premium")){
key = Settings.PREMIUM_SECTION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@SuppressWarnings("deprecation")
public class FeatureFlagsFragment extends Fragment {
ArrayList<FeatureFlag> flags;
private final String bundleFlagNameKey = "flagName";
private final String bundleFlagValueKey = "flagValue";

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,39 +50,43 @@ private void saveFlags() {
}

public void modifyFlag(FeatureFlagAdapter adapter, int position) {
FeatureFlag flag = flags.get(position);
try {
FeatureFlag flag = flags.get(position);

AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
dia.setTitle(Utils.getResourceString("piko_pref_edit_flag_title"));
AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
dia.setTitle(Utils.getResourceString("piko_pref_edit_flag_title"));

LinearLayout ln = new LinearLayout(getContext());
ln.setPadding(50, 50, 50, 50);
LinearLayout ln = new LinearLayout(getContext());
ln.setPadding(50, 50, 50, 50);

EditText flagEditText = new EditText(getContext());
flagEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
flagEditText.setText(flag.getName());
ln.addView(flagEditText);
EditText flagEditText = new EditText(getContext());
flagEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
flagEditText.setText(flag.getName());
ln.addView(flagEditText);

dia.setPositiveButton(Utils.getResourceString("save"), (dialogInterface, i) -> {
String editTextValue = flagEditText.getText().toString();
if (!editTextValue.equals(flag.getName())) {
flags.set(position, new FeatureFlag(flagEditText.getText().toString(), flag.getEnabled()));
dia.setPositiveButton(Utils.getResourceString("save"), (dialogInterface, i) -> {
String editTextValue = flagEditText.getText().toString();
if (!editTextValue.equals(flag.getName())) {
flags.set(position, new FeatureFlag(flagEditText.getText().toString(), flag.getEnabled()));
adapter.notifyDataSetChanged();
saveFlags();
}
});

dia.setNeutralButton(Utils.getResourceString("remove"), ((dialogInterface, i) -> {
flags.remove(position);
adapter.notifyDataSetChanged();
saveFlags();
}
});
}));

dia.setNeutralButton(Utils.getResourceString("remove"), ((dialogInterface, i) -> {
flags.remove(position);
adapter.notifyDataSetChanged();
saveFlags();
}));

dia.setNegativeButton(Utils.getResourceString("cancel"), null);
dia.setNegativeButton(Utils.getResourceString("cancel"), null);

dia.setView(ln);
dia.setView(ln);

dia.create().show();
dia.create().show();
} catch (Exception exception){
app.revanced.integrations.twitter.Utils.toast(exception.toString());
}
}

private void searchFlagsDialog(FeatureFlagAdapter parentAdapter) {
Expand Down Expand Up @@ -125,22 +131,28 @@ public void afterTextChanged(Editable editable) {
dialog.show();
}

public void addFlag(FeatureFlagAdapter adapter) {
public void addFlag(FeatureFlagAdapter adapter, Bundle bundle) {
boolean defaultFlagValue = true;
AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
dia.setTitle(Utils.getResourceString("piko_pref_add_flag_title"));

LinearLayout ln = new LinearLayout(getContext());
ln.setPadding(50, 50, 50, 50);

EditText flagEditText = new EditText(getContext());
// If flag is sent via deepLink.
if (bundle.containsKey(bundleFlagNameKey)) { // If flag is sent.
flagEditText.setText(bundle.getString(bundleFlagNameKey));
defaultFlagValue = bundle.getBoolean(bundleFlagValueKey, defaultFlagValue);
}
// TODO: add string to resources
flagEditText.setHint("flag");
flagEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ln.addView(flagEditText);

final boolean flagValue = defaultFlagValue;
dia.setPositiveButton(Utils.getResourceString("save"), (dialogInterface, i) -> {
String editTextValue = flagEditText.getText().toString();
flags.add(new FeatureFlag(editTextValue, true));
flags.add(new FeatureFlag(editTextValue, flagValue));
adapter.notifyDataSetChanged();
saveFlags();
});
Expand All @@ -159,6 +171,8 @@ public void addFlag(FeatureFlagAdapter adapter) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

Bundle bundle = getArguments(); // Required for deepLink flag addition.
View view = inflater.inflate(Utils.getResourceIdentifier("feature_flags_view", "layout"), container, false);
FloatingActionButton floatingActionButton = view.findViewById(Utils.getResourceIdentifier("add_flag", "id"));

Expand All @@ -172,7 +186,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
app.revanced.integrations.twitter.Utils.toast(adapter.getItem(i).getName());
});

floatingActionButton.setOnClickListener(view1 -> addFlag(adapter));
floatingActionButton.setOnClickListener(view1 -> addFlag(adapter, bundle));

adapter.setItemClickListener(position -> modifyFlag(adapter, position));

Expand All @@ -184,6 +198,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

rc.setAdapter(adapter);

// If flag is sent via deepLink.
if (bundle.containsKey(bundleFlagNameKey)) { // If flag is sent.
addFlag(adapter, bundle);
bundle.remove(bundleFlagNameKey);
bundle.remove(bundleFlagValueKey);
}

return view;
}
}

0 comments on commit 2c8bd5e

Please sign in to comment.