<commit_before>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */

package com.facebook.flipper.plugins.sharedpreferences;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SharedPreferencesFlipperPlugin implements FlipperPlugin {

  private FlipperConnection mConnection;
  private final Map<SharedPreferences, SharedPreferencesDescriptor> mSharedPreferences;
  private final SharedPreferences.OnSharedPreferenceChangeListener
      onSharedPreferenceChangeListener =
          new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
              if (mConnection == null) {
                return;
              }
              SharedPreferencesDescriptor descriptor = mSharedPreferences.get(sharedPreferences);
              if (descriptor == null) {
                return;
              }
              mConnection.send(
                  "sharedPreferencesChange",
                  new FlipperObject.Builder()
                      .put("preferences", descriptor.name)
                      .put("name", key)
                      .put("deleted", !sharedPreferences.contains(key))
                      .put("time", System.currentTimeMillis())
                      .put("value", sharedPreferences.getAll().get(key))
                      .build());
            }
          };

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from. Will use the package name as the file
   *     name with {@link Context#MODE_PRIVATE}.
   */
  public SharedPreferencesFlipperPlugin(Context context) {
    this(context, context.getPackageName());
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from. Will use the name as the file name with
   *     {@link Context#MODE_PRIVATE}.
   * @param name The preference file name.
   */
  public SharedPreferencesFlipperPlugin(Context context, String name) {
    this(context, name, MODE_PRIVATE);
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from.
   * @param name The preference file name.
   * @param mode The Context mode to utilize.
   */
  public SharedPreferencesFlipperPlugin(Context context, String name, int mode) {
    this(context, Arrays.asList(new SharedPreferencesDescriptor(name, mode)));
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the preferences from.
   * @param descriptors A list of {@link SharedPreferencesDescriptor}s that describe the list of
   *     preferences to retrieve.
   */
  public SharedPreferencesFlipperPlugin(
      Context context, List<SharedPreferencesDescriptor> descriptors) {
    if (context == null) {
      throw new IllegalArgumentException("Given null context");
    }
    mSharedPreferences = new HashMap<>(descriptors.size());
    for (SharedPreferencesDescriptor descriptor : descriptors) {
      SharedPreferences preferences =
          context.getSharedPreferences(descriptor.name, descriptor.mode);
      preferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
      mSharedPreferences.put(preferences, descriptor);
    }
  }

  @Override
  public String getId() {
    return "Preferences";
  }

  private SharedPreferences getSharedPreferencesFor(String name) {
    for (Map.Entry<SharedPreferences, SharedPreferencesDescriptor> entry :
        mSharedPreferences.entrySet()) {
      if (entry.getValue().name.equals(name)) {
        return entry.getKey();
      }
    }
    throw new IllegalStateException("Unknown shared preferences " + name);
  }

  private FlipperObject getFlipperObjectFor(String name) {
    return getFlipperObjectFor(getSharedPreferencesFor(name));
  }

  private FlipperObject getFlipperObjectFor(SharedPreferences sharedPreferences) {
    FlipperObject.Builder builder = new FlipperObject.Builder();
    Map<String, ?> map = sharedPreferences.getAll();
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      final Object val = entry.getValue();
      builder.put(entry.getKey(), val);
    }
    return builder.build();
  }
<commit_msg>SharedPreferencesPlugin -- Look for all shared pref files<commit_after>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */

package com.facebook.flipper.plugins.sharedpreferences;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SharedPreferencesFlipperPlugin implements FlipperPlugin {

  private static final String SHARED_PREFS_DIR = "shared_prefs";
  private static final String XML_SUFFIX = ".xml";
  private FlipperConnection mConnection;
  private final Map<SharedPreferences, SharedPreferencesDescriptor> mSharedPreferences;
  private final SharedPreferences.OnSharedPreferenceChangeListener
      onSharedPreferenceChangeListener =
          new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
              if (mConnection == null) {
                return;
              }
              SharedPreferencesDescriptor descriptor = mSharedPreferences.get(sharedPreferences);
              if (descriptor == null) {
                return;
              }
              mConnection.send(
                  "sharedPreferencesChange",
                  new FlipperObject.Builder()
                      .put("preferences", descriptor.name)
                      .put("name", key)
                      .put("deleted", !sharedPreferences.contains(key))
                      .put("time", System.currentTimeMillis())
                      .put("value", sharedPreferences.getAll().get(key))
                      .build());
            }
          };

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from. Will use the package name as the file
   *     name with {@link Context#MODE_PRIVATE}.
   */
  public SharedPreferencesFlipperPlugin(Context context) {
    this(context, buildDescriptorForAllPrefsFiles(context));
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from. Will use the name as the file name with
   *     {@link Context#MODE_PRIVATE}.
   * @param name The preference file name.
   */
  public SharedPreferencesFlipperPlugin(Context context, String name) {
    this(context, name, MODE_PRIVATE);
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the file from.
   * @param name The preference file name.
   * @param mode The Context mode to utilize.
   */
  public SharedPreferencesFlipperPlugin(Context context, String name, int mode) {
    this(context, Arrays.asList(new SharedPreferencesDescriptor(name, mode)));
  }

  /**
   * Creates a {@link android.content.SharedPreferences} plugin for Flipper
   *
   * @param context The context to retrieve the preferences from.
   * @param descriptors A list of {@link SharedPreferencesDescriptor}s that describe the list of
   *     preferences to retrieve.
   */
  public SharedPreferencesFlipperPlugin(
      Context context, List<SharedPreferencesDescriptor> descriptors) {
    if (context == null) {
      throw new IllegalArgumentException("Given null context");
    }
    mSharedPreferences = new HashMap<>(descriptors.size());
    for (SharedPreferencesDescriptor descriptor : descriptors) {
      SharedPreferences preferences =
          context.getSharedPreferences(descriptor.name, descriptor.mode);
      preferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
      mSharedPreferences.put(preferences, descriptor);
    }
  }

  @Override
  public String getId() {
    return "Preferences";
  }

  private static List<SharedPreferencesDescriptor> buildDescriptorForAllPrefsFiles(
      Context context) {
    File dir = new File(context.getApplicationInfo().dataDir, SHARED_PREFS_DIR);
    String[] list =
        dir.list(
            new FilenameFilter() {
              @Override
              public boolean accept(File dir, String name) {
                return name.endsWith(XML_SUFFIX);
              }
            });
    List<SharedPreferencesDescriptor> descriptors = new ArrayList<>();
    if (list != null) {
      for (String each : list) {
        String prefName = each.substring(0, each.indexOf(XML_SUFFIX));
        descriptors.add(new SharedPreferencesDescriptor(prefName, MODE_PRIVATE));
      }
    }
    return descriptors;
  }

  private SharedPreferences getSharedPreferencesFor(String name) {
    for (Map.Entry<SharedPreferences, SharedPreferencesDescriptor> entry :
        mSharedPreferences.entrySet()) {
      if (entry.getValue().name.equals(name)) {
        return entry.getKey();
      }
    }
    throw new IllegalStateException("Unknown shared preferences " + name);
  }

  private FlipperObject getFlipperObjectFor(String name) {
    return getFlipperObjectFor(getSharedPreferencesFor(name));
  }

  private FlipperObject getFlipperObjectFor(SharedPreferences sharedPreferences) {
    FlipperObject.Builder builder = new FlipperObject.Builder();
    Map<String, ?> map = sharedPreferences.getAll();
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      final Object val = entry.getValue();
      builder.put(entry.getKey(), val);
    }
    return builder.build();
  }

{'fraction_non_alphanumeric': 0.08942272561198393, 'fraction_numerical': 0.0008220679576178298, 'mean_word_length': 3.186998087954111, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1225462', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2793, 'n_words': 737}