156 lines
5.7 KiB
Java
156 lines
5.7 KiB
Java
package com.hainaos.vc.adapter;
|
|
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.text.TextUtils;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
import androidx.fragment.app.FragmentActivity;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.hainaos.vc.R;
|
|
import com.hainaos.vc.activity.category.list.CategoryListActivity;
|
|
import com.hainaos.vc.activity.user.UserActivity;
|
|
import com.hainaos.vc.bean.HomeAppInfo;
|
|
import com.hainaos.vc.utils.ApkUtils;
|
|
|
|
import java.util.List;
|
|
|
|
import me.jessyan.autosize.AutoSizeCompat;
|
|
|
|
public class HomeAppAdapter extends RecyclerView.Adapter<HomeAppAdapter.Holder> {
|
|
public static final String USER_CENTER = "user_center";
|
|
public static final String DOWNLOAD_CENTER = "download_center";
|
|
|
|
private FragmentActivity mContext;
|
|
|
|
private List<HomeAppInfo> mHomeAppInfos;
|
|
|
|
public void setHomeAppInfos(List<HomeAppInfo> homeAppInfos) {
|
|
mHomeAppInfos = homeAppInfos;
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
|
|
@NonNull
|
|
@Override
|
|
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
mContext = (FragmentActivity) parent.getContext();
|
|
AutoSizeCompat.autoConvertDensityOfGlobal(mContext.getResources());
|
|
Holder holder = new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false));
|
|
return holder;
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull Holder holder, int position) {
|
|
AutoSizeCompat.autoConvertDensityOfGlobal(mContext.getResources());
|
|
HomeAppInfo homeAppInfo = mHomeAppInfos.get(position);
|
|
String name = homeAppInfo.getName();
|
|
String dirName = homeAppInfo.getDirName();
|
|
String packageName = homeAppInfo.getPackageName();
|
|
String className = homeAppInfo.getClassName();
|
|
if (!TextUtils.isEmpty(name)) {
|
|
holder.tv_app_name.setText(name);
|
|
}
|
|
|
|
boolean dir = homeAppInfo.isDir();
|
|
if (dir) {
|
|
switch (packageName) {
|
|
case USER_CENTER:
|
|
holder.iv_icon.setImageDrawable(mContext.getDrawable(R.drawable.icon_user_center));
|
|
break;
|
|
case DOWNLOAD_CENTER:
|
|
holder.iv_icon.setImageDrawable(mContext.getDrawable(R.drawable.icon_download_manager));
|
|
break;
|
|
default:
|
|
holder.iv_icon.setImageDrawable(mContext.getDrawable(R.drawable.icon_category));
|
|
break;
|
|
}
|
|
} else {
|
|
holder.iv_icon.setImageDrawable(getIconByPackageNameAndClassName(mContext, packageName, className));
|
|
}
|
|
|
|
|
|
holder.root.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (dir) {
|
|
switch (packageName) {
|
|
case USER_CENTER:
|
|
mContext.startActivity(new Intent(mContext, UserActivity.class));
|
|
break;
|
|
case DOWNLOAD_CENTER:
|
|
mContext.startActivity(new Intent(mContext, CategoryListActivity.class));
|
|
break;
|
|
default:
|
|
// Intent intent = new Intent(mContext, LocalCategoryActivity.class);
|
|
// intent.putExtra("title", name);
|
|
// intent.putExtra("dir", dirName);
|
|
// mContext.startActivity(intent);
|
|
break;
|
|
}
|
|
} else {
|
|
if (TextUtils.isEmpty(className)) {
|
|
ApkUtils.openPackage(mContext, packageName);
|
|
} else {
|
|
ApkUtils.openPackage(mContext, packageName, className);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public Drawable getIconByPackageNameAndClassName(Context context, String packageName, String className) {
|
|
PackageManager pm = context.getPackageManager();
|
|
ComponentName cn = new ComponentName(packageName, className);
|
|
try {
|
|
ActivityInfo activityInfo = pm.getActivityInfo(cn, 0);
|
|
return activityInfo.loadIcon(pm);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
e.printStackTrace();
|
|
// 可以返回一个默认图标或者null
|
|
ApplicationInfo applicationInfo = null;
|
|
try {
|
|
applicationInfo = pm.getApplicationInfo(packageName, 0);
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
if (applicationInfo != null) {
|
|
return applicationInfo.loadLogo(pm);
|
|
}
|
|
return context.getDrawable(R.mipmap.ic_launcher);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return mHomeAppInfos == null ? 0 : mHomeAppInfos.size();
|
|
}
|
|
|
|
public class Holder extends RecyclerView.ViewHolder {
|
|
ConstraintLayout root;
|
|
TextView tv_app_name;
|
|
ImageView iv_icon;
|
|
|
|
public Holder(@NonNull View itemView) {
|
|
super(itemView);
|
|
|
|
root = itemView.findViewById(R.id.root);
|
|
tv_app_name = itemView.findViewById(R.id.tv_app_name);
|
|
iv_icon = itemView.findViewById(R.id.iv_icon);
|
|
}
|
|
}
|
|
}
|