Skip to content

Commit

Permalink
Merge pull request #21 from ukanth/beta
Browse files Browse the repository at this point in the history
Add icons to Favorite
  • Loading branch information
ukanth authored Oct 2, 2023
2 parents 9ae037c + c80e851 commit 14dea3b
Show file tree
Hide file tree
Showing 20 changed files with 657 additions and 27 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="dev.ukanth.iconmgr.MainActivity" />
</activity>

<activity android:name=".FavoriteAcitvity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="dev.ukanth.iconmgr.MainActivity" />
</activity>
<activity
android:name=".IconSearchActivity"
android:exported="false"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/dev/ukanth/iconmgr/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.Intent;
import android.content.IntentFilter;

import dev.ukanth.iconmgr.dao.FavDao;
import dev.ukanth.iconmgr.dao.FavDatabase;
import dev.ukanth.iconmgr.dao.HistoryDao;
import dev.ukanth.iconmgr.dao.HistoryDatabase;
import dev.ukanth.iconmgr.dao.IPObjDao;
Expand All @@ -23,6 +25,7 @@ public class App extends Application {

private HistoryDao historyDao;

private FavDao favDao;
private BroadcastReceiver receiver;


Expand Down Expand Up @@ -51,6 +54,8 @@ public void onCreate() {
HistoryDatabase db2 = HistoryDatabase.getInstance(getApplicationContext());
historyDao = db2.historyDao();

FavDatabase db3 = FavDatabase.getInstance(getApplicationContext());
favDao = db3.favDao();
}

public IPObjDao getIPObjDao() {
Expand All @@ -61,4 +66,6 @@ public HistoryDao getHistoryDao() {
return historyDao;
}

public FavDao getFavDao(){return favDao; }

}
160 changes: 160 additions & 0 deletions app/src/main/java/dev/ukanth/iconmgr/FavoriteAcitvity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package dev.ukanth.iconmgr;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.afollestad.materialdialogs.MaterialDialog;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dev.ukanth.iconmgr.dao.FavDao;


public class FavoriteAcitvity extends AppCompatActivity {
FavDao favDao = App.getInstance().getFavDao();
private Context mContext;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
mContext = FavoriteAcitvity.this;
setupActionBar();
LinearLayout iconPackContainer = findViewById(R.id.icon_pack_container);
List<String> iconName = favDao.geticonName();


if (Prefs.isDarkTheme()) {
setTheme(R.style.AppTheme_Dark);
} else {
setTheme(R.style.AppTheme_Light);
}

// Initialize a map to store the grouped icon images
Map<String, List<byte[]>> groupedIconImages = new HashMap<>();
// Group the icon images by iconName
for (String iconname : iconName) {
List<byte[]> iconImages = favDao.getIconImageData(iconname);
groupedIconImages.put(iconname, iconImages);
}

for (String icon : iconName) {
// Create a TextView for the current icon name
TextView textView = new TextView(this);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); // Text size
textView.setText(icon);
// Add the TextView to the icon pack container
iconPackContainer.addView(textView);
// Create a GridLayout for the associated icon images
GridLayout iconGridLayout = new GridLayout(this);
iconGridLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
// Set the number of columns for the GridLayout
int numColumns = Prefs.getCol(); // same no of column as settings
iconGridLayout.setColumnCount(numColumns);
// Get the list of icon images for the current iconName
List<byte[]> iconImages = groupedIconImages.get(icon);
// Display the icon images under the TextView
for (byte[] iconImageData : iconImages) {
Bitmap bitmap = BitmapFactory.decodeByteArray(iconImageData, 0, iconImageData.length);

String icontitle = favDao.getIcontitleForIconImageData(iconImageData);

ImageView image = new ImageView(getApplicationContext());
image.setPadding(15, 15, 15, 15);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setImageDrawable(new BitmapDrawable(getResources(), bitmap));

image.setOnClickListener(v -> {
View dialogView = LayoutInflater.from(mContext).inflate(R.layout.favorite_dialog, null);
ImageView saveImageView = dialogView.findViewById(R.id.saveImageView);
ImageView closeImageView = dialogView.findViewById(R.id.closeImageView);
ImageView removeImageView = dialogView.findViewById(R.id.removeImageView);
TextView titletextView = dialogView.findViewById(R.id.icon_title);

MaterialDialog dialog = new MaterialDialog.Builder(mContext)
.customView(dialogView, false)
.show();

titletextView.setText("Action for"+" "+ icontitle );

saveImageView.setOnClickListener(view -> {
File mediaDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (mediaDir == null) {
Log.e("MICO", "External storage not available");
return;
}
File myDir = new File(mediaDir, "micopacks/general/");
if (!myDir.exists() && !myDir.mkdirs()) {
Log.e("MICO", "Failed to create directory: " + myDir.getAbsolutePath());
return;
}
String fname = icontitle + ".png";
File file = new File(myDir, fname);
if (file.exists()) {
boolean deleted = file.delete();
if (deleted) {
Toast.makeText(getApplicationContext(), "Image deleted successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed to delete image", Toast.LENGTH_SHORT).show();
}
return;
}
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "Icon saved successfully: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("MICO", e.getMessage(), e);
}
});

removeImageView.setOnClickListener(view -> {
favDao.deleteIcon(iconImageData);
Toast.makeText(mContext, "Removed from favorites: " , Toast.LENGTH_SHORT).show();

});

closeImageView.setOnClickListener(view -> {
dialog.dismiss();
});

});
// Add the ImageView to the GridLayout
iconGridLayout.addView(image);
}
// Add the GridLayout to the icon pack container
iconPackContainer.addView(iconGridLayout);

}
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
81 changes: 69 additions & 12 deletions app/src/main/java/dev/ukanth/iconmgr/IconPreviewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
Expand All @@ -30,13 +31,16 @@
import com.glidebitmappool.GlideBitmapPool;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import dev.ukanth.iconmgr.dao.FavDao;
import dev.ukanth.iconmgr.dao.Favorite;
import dev.ukanth.iconmgr.dao.IPObj;
import dev.ukanth.iconmgr.dao.IPObjDao;
import dev.ukanth.iconmgr.util.LauncherHelper;
Expand Down Expand Up @@ -66,6 +70,8 @@ public class IconPreviewActivity extends AppCompatActivity {

IPObjDao ipObjDao = App.getInstance().getIPObjDao();

FavDao favDao = App.getInstance().getFavDao();

@Override
protected void onCreate(Bundle savedInstanceState) {

Expand Down Expand Up @@ -320,23 +326,74 @@ protected void onPostExecute(Boolean aBoolean) {
relativeLayout.setLayoutParams(params);
TextView textView = new TextView(mContext);
textView.setText(icon.getPackageName());*/

LayoutInflater inflater = LayoutInflater.from(mContext);
View dialogView = inflater.inflate(R.layout.custom_dailog,null);

ImageView dialogIconimage = dialogView.findViewById(R.id.icon_image);
dialogIconimage.setImageDrawable(new BitmapDrawable(getResources(), icon.getIconBitmap()));


ImageView image = new ImageView(mContext);
image.setLayoutParams(params);
image.setPadding(15, 15, 15, 15);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setImageDrawable(new BitmapDrawable(getResources(), icon.getIconBitmap()));
image.setOnClickListener(v -> new MaterialDialog.Builder(mContext)
.title(icon.getTitle())
.positiveText(R.string.save)
.onPositive((dialog, which) -> {
if (isStoragePermissionGranted()) {
saveImage(icon, icon.getPackageName());
dialog.dismiss();
}
})
.negativeText(R.string.close)
.icon(new BitmapDrawable(getResources(), icon.getIconBitmap()))
.show());

TextView titleTextView = dialogView.findViewById(R.id.title_text_view);
titleTextView.setText(icon.getTitle());

ImageView download = dialogView.findViewById(R.id.download);
ImageView close = dialogView.findViewById(R.id.close);
ImageView fav = dialogView.findViewById(R.id.favorite);

MaterialDialog dialog = new MaterialDialog.Builder(mContext) // set dailog view to custom_dailog
.customView(dialogView, true)
.build();
// Set the favorite status initially when the dialog is built
boolean Favorite = favDao.isFavorite(packageName, icon.getTitle(), iconName);
if (Favorite) {
fav.setImageResource(R.drawable.fav_filled);
} else {
fav.setImageResource(R.drawable.fav_border);

}

image.setOnClickListener(v -> dialog.show());


download.setOnClickListener(v -> {
if (isStoragePermissionGranted()) {
saveImage(icon, icon.getPackageName());
}
});

close.setOnClickListener(v -> {
// Dismiss the dialog when the close_button is clicked
dialog.dismiss();
});
fav.setOnClickListener(v -> {
boolean isFavorite = favDao.isFavorite(packageName, icon.getTitle(), iconName);
isFavorite = !isFavorite;
if (isFavorite == true) {
fav.setImageResource(R.drawable.fav_filled);
String Icontitle = icon.getTitle();
// Convert Drawable to Bitmap
Bitmap iconBitmap = ((BitmapDrawable) dialogIconimage.getDrawable()).getBitmap();
// Convert Bitmap to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
iconBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] iconImageData = stream.toByteArray();

Favorite favorite = new Favorite(packageName, iconName, Icontitle,iconImageData);
favDao.insertFavorite(favorite);

} else {
fav.setImageResource(R.drawable.fav_border);
favDao.deleteFavorite(packageName, icon.getTitle(), iconName);
}

});
image.setOnLongClickListener(view -> {
if (isStoragePermissionGranted()) {
saveImage(icon, packageName);
Expand Down
Loading

0 comments on commit 14dea3b

Please sign in to comment.