Skip to content

Commit

Permalink
Create a base icon parameter for FolderIcon class
Browse files Browse the repository at this point in the history
  • Loading branch information
falzonv committed Mar 2, 2024
1 parent 7c8e595 commit e6dd92c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ private void prepareFolders(Context context, boolean reversed)
}

// Create the folder icon with the number of applications inside and the selected color
Drawable icon = new FolderIcon(context, icon_size, folder.getApplications().size(), folder.getColor()) ;
Drawable baseIcon = AppCompatResources.getDrawable(context, R.drawable.icon_folder) ;
Drawable icon = new FolderIcon(baseIcon, icon_size, folder.getApplications().size(), folder.getColor()) ;
icon.setBounds(0, 0, icon_size, icon_size) ;
folder.setIcon(icon) ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

// Imports
import android.content.Context ;
import android.graphics.Bitmap ;
import android.graphics.Canvas ;
import android.graphics.ColorFilter ;
Expand All @@ -32,8 +31,7 @@
import android.graphics.PorterDuff ;
import android.graphics.PorterDuffColorFilter ;
import android.graphics.drawable.Drawable ;
import androidx.appcompat.content.res.AppCompatResources ;
import com.vincent_falzon.discreetlauncher.R ;
import androidx.annotation.NonNull ;

/**
* Create a colored folder icon containing the number of elements inside the folder.
Expand All @@ -50,17 +48,16 @@ public class FolderIcon extends Drawable
/**
* Constructor.
*/
public FolderIcon(Context context, int icon_size_pixels, int applications_number, int color)
public FolderIcon(Drawable baseIcon, int icon_size_pixels, int applications_number, int color)
{
// Retrieve the folder icon
// Prepare the base icon on which the number of apps will be written
icon_size = icon_size_pixels ;
Drawable folderIcon = AppCompatResources.getDrawable(context, R.drawable.icon_folder) ;
if(folderIcon != null)
if(baseIcon != null)
{
// Convert the folder icon into a Bitmap of the correct size
Bitmap convertedIcon = Bitmap.createBitmap(folderIcon.getIntrinsicWidth(), folderIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888) ;
folderIcon.setBounds(0, 0, icon_size, icon_size) ;
folderIcon.draw(new Canvas(convertedIcon)) ;
// Convert the base icon into a Bitmap of the correct size
Bitmap convertedIcon = Bitmap.createBitmap(baseIcon.getIntrinsicWidth(), baseIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888) ;
baseIcon.setBounds(0, 0, icon_size, icon_size) ;
baseIcon.draw(new Canvas(convertedIcon)) ;

// Get an editable copy of the Bitmap and change its color according to settings
icon = convertedIcon.copy(Bitmap.Config.ARGB_8888, true) ;
Expand All @@ -71,7 +68,7 @@ public FolderIcon(Context context, int icon_size_pixels, int applications_number
else icon = null ;

// Retrieve the number to write and define its settings
this.number = "" + applications_number ;
this.number = String.valueOf(applications_number) ;
paint = new Paint() ;
paint.setAntiAlias(true) ;
paint.setTextSize(icon_size / 3f) ;
Expand All @@ -84,9 +81,9 @@ public FolderIcon(Context context, int icon_size_pixels, int applications_number
* Draw the folder icon with the number of applications inside.
*/
@Override
public void draw(Canvas canvas)
public void draw(@NonNull Canvas canvas)
{
canvas.drawBitmap(icon, 0, 0, paint) ;
if(icon != null) canvas.drawBitmap(icon, 0, 0, paint) ;
canvas.drawText(number, icon_size * 0.5f, icon_size * 0.875f, paint) ;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import androidx.annotation.NonNull ;
import androidx.appcompat.app.AlertDialog ;
import androidx.appcompat.app.AppCompatActivity ;
import androidx.appcompat.content.res.AppCompatResources ;
import androidx.preference.PreferenceManager ;
import androidx.recyclerview.widget.LinearLayoutManager ;
import androidx.recyclerview.widget.RecyclerView ;
Expand Down Expand Up @@ -120,7 +121,8 @@ public void onClick(View view)
// Prepare the folder icon
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this) ;
int icon_size = Utils.getIconSize(this, settings) ;
Drawable icon = new FolderIcon(this, icon_size, 0, getResources().getColor(R.color.for_icon_added_in_drawer)) ;
Drawable baseIcon = AppCompatResources.getDrawable(this, R.drawable.icon_folder) ;
Drawable icon = new FolderIcon(baseIcon, icon_size, 0, getResources().getColor(R.color.for_icon_added_in_drawer)) ;
icon.setBounds(0, 0, icon_size, icon_size) ;

// Create the folder and update the list
Expand Down

0 comments on commit e6dd92c

Please sign in to comment.