/*
 * MIT License
 *
 * Copyright (c) 2017 Gwangseong Choi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package kstarchoi.recyclerviewbuilder;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.WeakHashMap;

import kstarchoi.lib.recyclerview.builder.DefaultViewBinder;
import kstarchoi.lib.recyclerview.builder.RecyclerViewBuilder;
import kstarchoi.lib.recyclerview.builder.ViewPreparer;
import kstarchoi.lib.recyclerview.builder.ViewProvider;

/**
 * @author Gwangseong Choi
 * @since 2017-07-23
 */

public class ApplicationExampleActivity extends AppCompatActivity {

    private WeakHashMap<String, Drawable> mIconDrawableMap = new WeakHashMap<>();
    private WeakHashMap<String, CharSequence> mLabelMap = new WeakHashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        new RecyclerViewBuilder<ResolveInfo>(recyclerView)
                .setViewBinder(new DefaultViewBinder<ResolveInfo>() {
                    @Override
                    public int getViewLayoutRes(int viewType) {
                        return R.layout.item_application;
                    }

                    @Override
                    public void init(ViewPreparer preparer) {
                        preparer.reserve(android.R.id.icon1,
                                android.R.id.text1, android.R.id.text2);
                    }

                    @Override
                    public void bind(ViewProvider provider, int index, ResolveInfo resolveInfo) {
                        ImageView iconView = provider.get(android.R.id.icon1);
                        iconView.setImageDrawable(getIconDrawable(resolveInfo));

                        TextView nameView = provider.get(android.R.id.text1);
                        nameView.setText(getLabel(resolveInfo));

                        TextView packageNameView = provider.get(android.R.id.text2);
                        packageNameView.setText(resolveInfo.activityInfo.packageName);
                    }
                })
                .addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
                .build(getCategoryLauncherResolveInfoList());
    }

    private List<ResolveInfo> getCategoryLauncherResolveInfoList() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        return getPackageManager().queryIntentActivities(intent, 0);
    }

    private Drawable getIconDrawable(ResolveInfo resolveInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        if (mIconDrawableMap.containsKey(packageName)) {
            return mIconDrawableMap.get(packageName);
        }

        Drawable iconDrawable = resolveInfo.loadIcon(getPackageManager());
        mIconDrawableMap.put(packageName, iconDrawable);
        return iconDrawable;
    }

    private CharSequence getLabel(ResolveInfo resolveInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        if (mLabelMap.containsKey(packageName)) {
            return mLabelMap.get(packageName);
        }

        CharSequence label = resolveInfo.loadLabel(getPackageManager());
        mLabelMap.put(packageName, label);
        return label;
    }
}

@@ -1203,16 +1203,56 @@
 Intent;%0A
+import android.content.pm.ActivityInfo;%0A
 import a
@@ -4101,34 +4101,41 @@
         
-String packageName
+ActivityInfo activityInfo
  = resol
@@ -4149,33 +4149,70 @@
 activityInfo
-.packageN
+;%0A        String activityName = activityInfo.n
 ame;%0A       
@@ -4237,39 +4237,40 @@
 Map.containsKey(
-p
 ac
-kage
+tivity
 Name)) %7B%0A       
@@ -4294,39 +4294,40 @@
 DrawableMap.get(
-p
 ac
-kage
+tivity
 Name);%0A        %7D
@@ -4356,31 +4356,32 @@
 nDrawable = 
-resolve
+activity
 Info.loadIco
@@ -4429,31 +4429,32 @@
 ableMap.put(
-p
 ac
-kage
+tivity
 Name, iconDr
@@ -4571,26 +4571,33 @@
     
-String packageName
+ActivityInfo activityInfo
  = r
@@ -4619,25 +4619,62 @@
 vityInfo
-.packageN
+;%0A        String activityName = activityInfo.n
 ame;%0A   
@@ -4704,23 +4704,24 @@
 ainsKey(
-p
 ac
-kage
+tivity
 Name)) %7B
@@ -4754,23 +4754,24 @@
 Map.get(
-p
 ac
-kage
+tivity
 Name);%0A 
@@ -4809,23 +4809,24 @@
 label = 
-resolve
+activity
 Info.loa
@@ -4876,23 +4876,24 @@
 Map.put(
-p
 ac
-kage
+tivity
 Name, la

Resolve duplicated icon and label issue