Java Code Examples for com.gianlu.commonutils.preferences.Prefs#getSet()

The following examples show how to use com.gianlu.commonutils.preferences.Prefs#getSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptionsAdapter.java    From Aria2App with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
public static OptionsAdapter setup(@NonNull Context context, @NonNull OptionsMap map, boolean global, boolean quick, boolean quickOnTop, Listener listener) throws IOException, JSONException {
    List<String> all;
    if (global) all = OptionsManager.get(context).loadGlobalOptions();
    else all = OptionsManager.get(context).loadDownloadOptions();

    Set<String> filter;
    if (quick) filter = Prefs.getSet(PK.A2_QUICK_OPTIONS_MIXED, null);
    else filter = null;

    List<Option> options = Option.fromOptionsMap(map, all, filter);
    if (quickOnTop)
        Collections.sort(options, new OptionsManager.IsQuickComparator());

    return new OptionsAdapter(context, options, listener);
}
 
Example 2
Source File: FavoritesAdapter.java    From DNSHero with GNU General Public License v3.0 5 votes vote down vote up
public FavoritesAdapter(@NonNull Context context, Listener listener) {
    this.inflater = LayoutInflater.from(context);
    this.listener = listener;
    this.favorites = new ArrayList<>(Prefs.getSet(PK.FAVORITES, new HashSet<>()));

    Collections.sort(favorites);
}
 
Example 3
Source File: MainActivity.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
private void setupAdapterFiltersAndSorting() {
    if (adapter == null) return;

    List<Download.Status> filters = new ArrayList<>(Arrays.asList(Download.Status.values()));
    Set<String> checkedFiltersSet = Prefs.getSet(PK.A2_MAIN_FILTERS);
    for (String filter : checkedFiltersSet) filters.remove(Download.Status.valueOf(filter));
    adapter.setFilters(filters);
    adapter.sort(DownloadCardsAdapter.SortBy.valueOf(Prefs.getString(PK.A2_MAIN_SORTING)));

    updateFiltersVerbose();
}
 
Example 4
Source File: MainActivity.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
private void showFilteringDialog() {
    final Download.Status[] filters = new Download.Status[]{Download.Status.ACTIVE, Download.Status.PAUSED, Download.Status.WAITING, Download.Status.ERROR, Download.Status.REMOVED, Download.Status.COMPLETE};
    CharSequence[] stringFilters = new CharSequence[filters.length];

    for (int i = 0; i < filters.length; i++)
        stringFilters[i] = filters[i].getFormal(this, true);

    final boolean[] checkedFilters = new boolean[filters.length];
    Set<String> checkedFiltersSet = Prefs.getSet(PK.A2_MAIN_FILTERS);

    for (String checkedFilter : checkedFiltersSet) {
        Download.Status filter = Download.Status.valueOf(checkedFilter);
        int pos = CommonUtils.indexOf(filters, filter);
        if (pos != -1) checkedFilters[pos] = true;
    }

    AlertDialog.Builder builder = new MaterialAlertDialogBuilder(this);
    builder.setTitle(R.string.filters)
            .setMultiChoiceItems(stringFilters, checkedFilters, (dialog, which, isChecked) -> checkedFilters[which] = isChecked)
            .setPositiveButton(R.string.apply, (dialog, which) -> {
                List<Download.Status> toApplyFilters = new ArrayList<>();
                for (int i = 0; i < checkedFilters.length; i++)
                    if (!checkedFilters[i]) toApplyFilters.add(filters[i]);

                if (adapter != null) adapter.setFilters(toApplyFilters);
                Set<String> set = new HashSet<>();
                for (int i = 0; i < checkedFilters.length; i++)
                    if (checkedFilters[i]) set.add(filters[i].name());

                Prefs.putSet(PK.A2_MAIN_FILTERS, set);

                updateFiltersVerbose();
            })
            .setNegativeButton(android.R.string.cancel, null);

    showDialog(builder);
}
 
Example 5
Source File: MainActivity.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
private void updateFiltersVerbose() {
    Set<String> filters = Prefs.getSet(PK.A2_MAIN_FILTERS);
    if (filters.size() == Download.Status.values().length) {
        filtersVerbose.setVisibility(View.GONE);
    } else {
        filtersVerbose.setVisibility(View.VISIBLE);
        filtersVerbose.setText(getString(R.string.filtersShowingOnly, CommonUtils.join(filters, ", ",
                obj -> Download.Status.valueOf(obj).getFormal(MainActivity.this, true)),
                adapter == null ? 0 : adapter.getItemCount()));
    }
}
 
Example 6
Source File: TrackersListFetch.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
public void getTrackers(@NonNull Type type, @Nullable Activity activity, @NonNull Listener listener) {
    if (Prefs.has(PK.TRACKERS_LIST_CACHE) && !CommonUtils.isDebug()) {
        long age = Prefs.getLong(PK.TRACKERS_LIST_CACHE_AGE, 0);
        if (System.currentTimeMillis() - age < TimeUnit.DAYS.toMillis(1)) {
            Set<String> set = Prefs.getSet(PK.TRACKERS_LIST_CACHE, null);
            if (set != null && set.size() > 0)
                listener.onDone(new ArrayList<>(set));
        }
    }

    executorService.execute(new LifecycleAwareRunnable(handler, activity == null ? listener : activity) {
        @Override
        public void run() {
            try {
                Response resp = client.newCall(new Request.Builder().get().url(String.format(BASE_URL, type.getId())).build()).execute();
                if (resp.code() != 200)
                    throw new IOException(resp.code() + ": " + resp.message());

                ResponseBody body = resp.body();
                if (body == null)
                    throw new IOException("Body is empty!");

                List<String> trackers = new ArrayList<>();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null && !line.isEmpty())
                        trackers.add(line);
                }

                Prefs.putLong(PK.TRACKERS_LIST_CACHE_AGE, System.currentTimeMillis());
                Prefs.putSet(PK.TRACKERS_LIST_CACHE, new HashSet<>(trackers));

                post(() -> listener.onDone(trackers));
            } catch (IOException ex) {
                post(() -> listener.onFailed(ex));
            }
        }
    });
}
 
Example 7
Source File: SearchActivity.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
private void showEnginesDialog(@NotNull List<SearchEngine> engines) {
    CharSequence[] enginesNames = new CharSequence[engines.size()];

    for (int i = 0; i < engines.size(); i++) {
        SearchEngine engine = engines.get(i);
        enginesNames[i] = engine.name + (engine.proxyed ? " (proxyed)" : "");
    }

    final boolean[] checkedEngines = new boolean[engines.size()];
    Set<String> checkedEnginesSet = Prefs.getSet(PK.A2_SEARCH_ENGINES, null);

    if (checkedEnginesSet == null) {
        Arrays.fill(checkedEngines, true);
    } else {
        for (String checkedEngine : checkedEnginesSet)
            for (int i = 0; i < engines.size(); i++)
                if (Objects.equals(engines.get(i).id, checkedEngine))
                    checkedEngines[i] = true;
    }

    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
    builder.setTitle(R.string.searchEngines)
            .setMultiChoiceItems(enginesNames, checkedEngines, (dialog, which, isChecked) -> checkedEngines[which] = isChecked)
            .setPositiveButton(R.string.apply, (dialog, which) -> {
                Set<String> set = new HashSet<>();
                for (int i = 0; i < checkedEngines.length; i++)
                    if (checkedEngines[i]) set.add(engines.get(i).id);

                if (set.isEmpty()) {
                    Toaster.with(SearchActivity.this).message(R.string.noEnginesSelected).show();
                } else {
                    Prefs.putSet(PK.A2_SEARCH_ENGINES, set);
                    if (query != null) onQueryTextSubmit(query);
                }
            })
            .setNegativeButton(android.R.string.cancel, null);

    showDialog(builder);
}