package com.xxpatx.os.bean; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Parcel; import android.os.Parcelable; import androidx.annotation.NonNull; import com.google.gson.Gson; import com.google.gson.JsonParser; import com.xxpatx.os.BuildConfig; import com.xxpatx.os.R; import com.xxpatx.os.manager.AppManager; import com.xxpatx.os.utils.IconUtils; import java.io.Serializable; public class DesktopIcon implements Serializable, Parcelable { private static final long serialVersionUID = 3358230413497783708L; protected String mTitle; protected String mClass; protected String mPackage; int position; public DesktopIcon() { } private DesktopIcon(Parcel in) { mPackage = in.readString(); mTitle = in.readString(); position = in.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(mPackage); dest.writeString(mTitle); dest.writeInt(position); } @Override public int describeContents() { return 0; } public static final Creator CREATOR = new Creator() { @Override public DesktopIcon createFromParcel(Parcel in) { return new DesktopIcon(in); } @Override public DesktopIcon[] newArray(int size) { return new DesktopIcon[size]; } }; public String getPackage() { return mPackage; } public void setPackage(String aPackage) { this.mPackage = aPackage; } public String getClazz() { return mClass; } public void setClass(String aClass) { this.mClass = aClass; } public String getTitle() { return mTitle; } public void setTitle(String title) { this.mTitle = title; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public Drawable getIcon(Context context) { switch (mPackage) { case AppManager.ADD_NAME: return context.getDrawable(R.drawable.home_icon_add); case AppManager.SERVICE_PACKAGE: return context.getDrawable(R.drawable.icon_wechat_service); case AppManager.CONTACT_PACKAGE: return context.getDrawable(R.drawable.com_android_contacts); case AppManager.EXIT_DESKTOP: return context.getDrawable(R.drawable.exit_icon); case "aios.daily.app": return context.getDrawable(R.drawable.icon_daily_app); case "aios.appstore": return context.getDrawable(R.drawable.com_android_appstore); default: PackageManager pm = context.getPackageManager(); ApplicationInfo info = null; try { info = pm.getApplicationInfo(mPackage, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (info == null) { return null; } else { int i = IconUtils.appClassNameList.indexOf(mPackage); if (i != -1) { String val = IconUtils.appIconList.get(i); int resID = context.getResources().getIdentifier(val, "drawable", BuildConfig.APPLICATION_ID); if (resID != 0) { return context.getResources().getDrawable(resID); } } return info.loadIcon(pm); } } } public static DesktopIcon creatDesktopIcon(Context context, ResolveInfo resolveInfo) { PackageManager pm = context.getPackageManager(); DesktopIcon desktopIcon = new DesktopIcon(); desktopIcon.setPackage(resolveInfo.activityInfo.packageName); desktopIcon.setClass(resolveInfo.activityInfo.name); desktopIcon.setTitle(resolveInfo.loadLabel(pm).toString()); desktopIcon.setPosition(0); return desktopIcon; } public static DesktopIcon creatDesktopIcon(Context context, ResolveInfo resolveInfo, int position) { PackageManager pm = context.getPackageManager(); DesktopIcon desktopIcon = new DesktopIcon(); desktopIcon.setPackage(resolveInfo.activityInfo.packageName); desktopIcon.setClass(resolveInfo.activityInfo.name); desktopIcon.setTitle(resolveInfo.loadLabel(pm).toString()); desktopIcon.setPosition(position); return desktopIcon; } @NonNull @Override public String toString() { return JsonParser.parseString(new Gson().toJson(this)).getAsJsonObject().toString(); } }