package com.android.launcher3.settings;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;


public class AppModel {

    private final Context mContext;
    private final ApplicationInfo mInfo;

    private String mAppLabel;
    private Drawable mIcon;
    private int mAppType = APP_TYPE_USER;

    public static final int APP_TYPE_SYSTEM = 0;
    public static final int APP_TYPE_USER = 1;

    public AppModel(Context context, String label, Drawable icon, int appType) {
        this.mAppLabel = label;
        this.mIcon = icon;
        this.mAppType = appType;
        this.mContext = context;
        mInfo = null;
    }

    public AppModel(Context context, ApplicationInfo info) {
        mContext = context;
        mInfo = info;
        loadLabel();
        loadIcon();
    }

    public ApplicationInfo getAppInfo() {
        return mInfo;
    }

    public int getAppType() {
        return mAppType;
    }

    public String getApplicationPackageName() {
        return getAppInfo().packageName;
    }

    public String getLabel() {
        return mAppLabel;
    }

    public Drawable getIcon() {
        return mIcon;
    }

    private void loadIcon() {
        mIcon = mContext.getPackageManager().getApplicationIcon(mInfo);
        if (mIcon == null) {
            mIcon = mInfo.loadIcon(mContext.getPackageManager());
        }
    }

    private void loadLabel() {
        mAppLabel = mInfo.loadLabel(mContext.getPackageManager()).toString();
        if (TextUtils.isEmpty(mAppLabel)) {
            mAppLabel = mInfo.name;
        }
    }
}
