package com.seizonsenryaku.hayailauncher;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.Log;

public class LaunchableActivity implements Comparable<LaunchableActivity> {
	private ActivityInfo activityInfo;
	private int numberOfLaunches;
	private String activityLabel;
	private Drawable activityIcon;
	private boolean favorite;
    private ComponentName componentName;
	//This limitation is needed to speedup the compareTo function.
	private static final int MAX_LAUNCHES = 16383;

	public LaunchableActivity(ActivityInfo activityInfo, String activityLabel) {
		this.activityInfo = activityInfo;
		this.activityLabel = activityLabel;
        componentName = new ComponentName(activityInfo.packageName, activityInfo.name);
	}

	public void incrementLaunches() {
		if(numberOfLaunches<MAX_LAUNCHES)
			numberOfLaunches++;
	}

	public void setNumberOfLaunches(int numberOfLaunches) {
		this.numberOfLaunches = numberOfLaunches;
	}

	public void setFavorite(boolean favorite) {
		this.favorite = favorite;
	}

	public boolean isFavorite() {
		return favorite;
	}

	public int getNumberOfLaunches() {
		return numberOfLaunches;
	}

	public CharSequence getActivityLabel() {
		return activityLabel;
	}

	public Drawable getActivityIcon(PackageManager pm,Context context ) {

		if (activityIcon == null) {
			if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1){
				try {
                    final ActivityManager activityManager =
                            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                    final int iconDpi = activityManager.getLauncherLargeIconDensity();
                    activityIcon = pm.getResourcesForActivity(componentName).getDrawableForDensity(
                            activityInfo.getIconResource(), iconDpi);
				} catch (PackageManager.NameNotFoundException e) {
					e.printStackTrace();
				}
			}else {
				activityIcon = activityInfo.loadIcon(pm);
			}
		}
		return activityIcon;
	}

	@Override
	public int compareTo(@NonNull LaunchableActivity another) {
		
		//Criteria 1 (Bit 1) indicates whether the activity is flagged as favorite or not.
		//Criteria 2 (Bits 2 to 16) indicates the number of launches
		//Criteria 3 (Bits 17 to 31) indicates string difference (can be at most Character.MAX_VALUE)
		
		//
		int anotherN = (another.favorite ? 0x40000000
				: 0) + (another.numberOfLaunches<<16);
		int thisN = (this.favorite ? 0x40000000
				: 0) + (this.numberOfLaunches<<16);

		
		anotherN+=this.activityLabel.compareTo(another.activityLabel);

		return anotherN - thisN;
	}

	public ComponentName getComponent() {
		return componentName;
	}

	public String getClassName() {
		return activityInfo.name;
	}
}

@@ -216,32 +216,70 @@
 PackageManager;%0A
+import android.content.res.Resources;%0A
 import android.g
@@ -1717,22 +1717,8 @@
 1)%7B%0A
-%09%09%09%09try %7B%0A    
     
@@ -1793,20 +1793,16 @@
         
-  
-  
 (Activit
@@ -1875,28 +1875,24 @@
             
-    
 final int ic
@@ -1946,16 +1946,26 @@
 sity();%0A
+%09%09%09%09try %7B%0A
         
@@ -2213,16 +2213,236 @@
 ;%0A%09%09%09%09%7D%0A
+                if(activityIcon==null)%7B%0A                        activityIcon = Resources.getSystem().getDrawableForDensity(%0A                                android.R.mipmap.sym_def_app_icon, iconDpi);%0A                %7D%0A%0A
 %09%09%09%7Delse
@@ -2444,16 +2444,17 @@
 %7Delse %7B%0A
+%0A
 %09%09%09%09acti
@@ -2491,16 +2491,208 @@
 on(pm);%0A
+                if(activityIcon==null)%7B%0A                    activityIcon = Resources.getSystem().getDrawable(%0A                            android.R.mipmap.sym_def_app_icon);%0A                %7D%0A
 %09%09%09%7D%0A%09%09%7D

Load the default icon when none is available