Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ android {
applicationId "com.codedead.advancedpassgen"
minSdk 29
targetSdk 37
versionCode 5
versionName '1.0.4'
versionCode 6
versionName '1.1.0'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
optimization {
enable = true // Enables code and resource optimizations.
}
}
}
compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
Expand All @@ -14,21 +13,27 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.codedead.advancedpassgen.R;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class PasswordAdapter extends RecyclerView.Adapter<PasswordViewHolder> {

private static final String SORT_NONE = "none";
private static final String SORT_SHORTEST_FIRST = "shortest_first";
private static final String SORT_LONGEST_FIRST = "longest_first";

private final Context context;
private final List<PasswordItem> items;
private final List<PasswordItem> generationOrder;
private boolean displayStrength;
private PasswordViewHolder holder;
private String sortOrder;

/**
* Initialize a new PasswordAdapter
Expand All @@ -38,7 +43,9 @@ public class PasswordAdapter extends RecyclerView.Adapter<PasswordViewHolder> {
public PasswordAdapter(final Context context) {
this.context = context;
this.items = new ArrayList<>();
this.generationOrder = new ArrayList<>();
this.displayStrength = true;
this.sortOrder = SORT_NONE;
}

/**
Expand All @@ -49,15 +56,35 @@ public PasswordAdapter(final Context context) {
@SuppressLint("NotifyDataSetChanged")
public void insert(final List<PasswordItem> newItems) {
items.addAll(newItems);
generationOrder.addAll(newItems);

if (!SORT_NONE.equals(sortOrder)) {
sortItemsByLength();
}

notifyDataSetChanged();
}

/**
* Add an item to the adapter
*
* @param item The {@link PasswordItem} to add
* @return The position of the added item, or {@link RecyclerView#NO_POSITION}
*/
public void add(final PasswordItem item) {
@SuppressLint("NotifyDataSetChanged")
public int add(final PasswordItem item) {
items.add(item);
notifyItemInserted(items.size() - 1);
generationOrder.add(item);

if (SORT_NONE.equals(sortOrder)) {
final int position = items.size() - 1;
notifyItemInserted(position);
return position;
}

sortItemsByLength();
notifyDataSetChanged();
return indexOfIdentity(item);
}

/**
Expand All @@ -66,6 +93,36 @@ public void add(final PasswordItem item) {
@SuppressLint("NotifyDataSetChanged")
public void clear() {
items.clear();
generationOrder.clear();
notifyDataSetChanged();
}

/**
* Set the order in which passwords are displayed
*
* @param sortOrder One of "none", "shortest_first", or "longest_first"
*/
@SuppressLint("NotifyDataSetChanged")
public void setSortOrder(final String sortOrder) {
final String normalizedSortOrder = normalizeSortOrder(sortOrder);

if (normalizedSortOrder.equals(this.sortOrder)) {
return;
}

this.sortOrder = normalizedSortOrder;

if (items.isEmpty()) {
return;
}

if (SORT_NONE.equals(this.sortOrder)) {
items.clear();
items.addAll(generationOrder);
} else {
sortItemsByLength();
}

notifyDataSetChanged();
}

Expand All @@ -92,25 +149,20 @@ public PasswordViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, fi
*/
@Override
public void onBindViewHolder(@NonNull final PasswordViewHolder holder, final int position) {
this.holder = holder;
final PasswordItem item = items.get(position);
final int strength = item.strength();
final int strengthColor = ContextCompat.getColor(context, getStrengthColorRes(strength));
final int passwordLength = item.password().length();

holder.getPasswordTextView().setText(item.password());
holder.getStrengthProgressBar().setProgress(item.strength());

if (displayStrength) {
holder.getStrengthProgressBar().setVisibility(View.VISIBLE);
} else {
holder.getStrengthProgressBar().setVisibility(View.GONE);
}

if (item.strength() == 100) {
DrawableCompat.setTint(holder.getStrengthProgressBar().getProgressDrawable(), Color.GREEN);
} else if (item.strength() <= 35) {
DrawableCompat.setTint(holder.getStrengthProgressBar().getProgressDrawable(), Color.RED);
} else if (item.strength() <= 75) {
DrawableCompat.setTint(holder.getStrengthProgressBar().getProgressDrawable(), Color.parseColor("#FFA500"));
}
holder.getStrengthProgressBar().setProgressCompat(strength, false);
holder.getStrengthProgressBar().setIndicatorColor(strengthColor);
holder.getStrengthLabel().setText(getStrengthLabelRes(strength));
holder.getStrengthLabel().setTextColor(strengthColor);
holder.getLengthLabel().setText(context.getResources().getQuantityString(
R.plurals.password_length, passwordLength, passwordLength));
holder.getStrengthProgressBar().setVisibility(displayStrength ? View.VISIBLE : View.GONE);
holder.getStrengthLabel().setVisibility(displayStrength ? View.VISIBLE : View.GONE);

holder.getCopyButton().setOnClickListener(e -> {
// Copy the password to the clipboard
Expand Down Expand Up @@ -155,20 +207,97 @@ public List<PasswordItem> getItems() {
return new ArrayList<>(items);
}

/**
* Sort the displayed items by password length
*/
private void sortItemsByLength() {
final Comparator<PasswordItem> byLength = Comparator.comparingInt(
(PasswordItem item) -> item.password().length());

items.sort(SORT_LONGEST_FIRST.equals(sortOrder) ? byLength.reversed() : byLength);
}

/**
* Normalize a sort order value
*
* @param sortOrder The sort order value to normalize
* @return One of the supported sort order values
*/
private static String normalizeSortOrder(final String sortOrder) {
if (SORT_SHORTEST_FIRST.equals(sortOrder) || SORT_LONGEST_FIRST.equals(sortOrder)) {
return sortOrder;
}

return SORT_NONE;
}

/**
* Find the position of an item by object identity
*
* @param item The item to find
* @return The position of the item, or {@link RecyclerView#NO_POSITION}
*/
private int indexOfIdentity(final PasswordItem item) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i) == item) {
return i;
}
}

return RecyclerView.NO_POSITION;
}

/**
* Set the display strength setting
*
* @param displayStrength The display strength setting
*/
@SuppressLint("NotifyDataSetChanged")
public void setDisplayStrength(final boolean displayStrength) {
if (this.displayStrength == displayStrength) {
return;
}

this.displayStrength = displayStrength;
notifyDataSetChanged();
}

if (holder != null) {
if (displayStrength) {
holder.getStrengthProgressBar().setVisibility(View.VISIBLE);
} else {
holder.getStrengthProgressBar().setVisibility(View.GONE);
}
/**
* Get the color resource for a password strength
*
* @param strength The strength of the password
* @return The color resource identifier
*/
private static int getStrengthColorRes(final int strength) {
if (strength >= 100) {
return R.color.strength_strong;
}
if (strength >= 76) {
return R.color.strength_good;
}
if (strength >= 36) {
return R.color.strength_fair;
}
return R.color.strength_weak;
}

/**
* Get the label resource for a password strength
*
* @param strength The strength of the password
* @return The string resource identifier
*/
@StringRes
private static int getStrengthLabelRes(final int strength) {
if (strength >= 100) {
return R.string.strength_strong;
}
if (strength >= 76) {
return R.string.strength_good;
}
if (strength >= 36) {
return R.string.strength_fair;
}
return R.string.strength_weak;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.codedead.advancedpassgen.R;
import com.google.android.material.progressindicator.LinearProgressIndicator;

public class PasswordViewHolder extends RecyclerView.ViewHolder {

private final TextView passwordTextView;
private final ProgressBar strengthProgressBar;
private final LinearProgressIndicator strengthProgressBar;
private final TextView strengthLabel;
private final TextView lengthLabel;
private final ImageButton copyButton;

/**
Expand All @@ -26,6 +28,8 @@ public PasswordViewHolder(@NonNull final View itemView) {

passwordTextView = itemView.findViewById(R.id.password_text);
strengthProgressBar = itemView.findViewById(R.id.password_strength);
strengthLabel = itemView.findViewById(R.id.password_strength_label);
lengthLabel = itemView.findViewById(R.id.password_length);
copyButton = itemView.findViewById(R.id.copy_button);
}

Expand All @@ -39,14 +43,32 @@ public TextView getPasswordTextView() {
}

/**
* Get the strength {@link ProgressBar}
* Get the strength {@link LinearProgressIndicator}
*
* @return The strength {@link ProgressBar}
* @return The strength {@link LinearProgressIndicator}
*/
public ProgressBar getStrengthProgressBar() {
public LinearProgressIndicator getStrengthProgressBar() {
return strengthProgressBar;
}

/**
* Get the strength label {@link TextView}
*
* @return The strength label {@link TextView}
*/
public TextView getStrengthLabel() {
return strengthLabel;
}

/**
* Get the password length label {@link TextView}
*
* @return The password length label {@link TextView}
*/
public TextView getLengthLabel() {
return lengthLabel;
}

/**
* Get the copy {@link ImageButton}
*
Expand Down
Loading
Loading