package de.devmil.paperlaunch.utils;

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.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;

public abstract class AppMetadataUtils {

    public static String getAppName(Context context, Intent appIntent)
    {

        if(appIntent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
            return appIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
        }

        if(appIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
            appIntent = appIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
        }
        ComponentName componentName = appIntent.getComponent();

        PackageManager pm = context.getPackageManager();

        ApplicationInfo appInfo = null;
        ActivityInfo activityInfo = null;
        try {
            appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            appInfo = null;
        }
        try {
            activityInfo = pm.getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if(appInfo == null) {
            return null;
        } else {
            StringBuilder result = new StringBuilder();

            CharSequence appName = pm.getApplicationLabel(appInfo);
            if(appName != null) {
                result.append(appName);
            }

            if(activityInfo != null) {
                CharSequence activityName = activityInfo.loadLabel(pm);
                if(activityName != null && !activityName.equals(appName)) {
                    result.append(" - ").append(activityName);
                }
            }

            return result.toString();
        }
    }

    public static Drawable getAppIcon(Context context, Intent launchIntent)
    {
        Drawable result = null;

        if(launchIntent.hasExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE)) {
            result = getShortcutIcon(context, launchIntent);
        }

        if(result != null) {
            return result;
        }

        if(launchIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
            launchIntent = launchIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
        }

        PackageManager pm = context.getPackageManager();

        ApplicationInfo appInfo = null;
        try {
            appInfo = pm.getApplicationInfo(launchIntent.getComponent().getPackageName(), 0);
        } catch (Exception e) {
            appInfo = null;
        }
        if(appInfo == null) {
            return null;
        } else {
            return pm.getApplicationIcon(appInfo);
        }
    }

    private static Drawable getShortcutIcon(Context context, Intent shortcutIntent) {
        if(!shortcutIntent.hasExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE)) {
            return null;
        }
        try {
            Intent.ShortcutIconResource iconRes = shortcutIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
            Resources appRes = context.getPackageManager().getResourcesForApplication(iconRes.packageName);
            int resId = appRes.getIdentifier(iconRes.resourceName, null, null);
            return appRes.getDrawable(resId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

@@ -1444,65 +1444,8 @@
 e %7B%0A
-            StringBuilder result = new StringBuilder();%0A%0A
     
@@ -1524,83 +1524,41 @@
     
-if(appName != null) %7B%0A                result.append(appName);%0A            %7D
+CharSequence activityName = null;
 %0A%0A  
@@ -1606,37 +1606,24 @@
             
-CharSequence 
 activityName
@@ -1653,24 +1653,35 @@
 el(pm);%0A    
+        %7D%0A%0A
             
@@ -1707,44 +1707,59 @@
 null
- && !activityName.equals(appName)) %7B
+) %7B%0A                return activityName.toString();
 %0A   
@@ -1763,32 +1763,35 @@
             
+%7D%0A%0A
         
 result.appen
@@ -1782,68 +1782,69 @@
     
-result.append(%22 - %22).append(activityName);%0A                %7D
+    if(appName != null) %7B%0A                appName.toString();
 %0A   
@@ -1878,25 +1878,12 @@
 urn 
-result.toString()
+null
 ;%0A  

use activity name alone when there is one instead of combining it with the application name