package ireader.home;

import java.util.Collections;
import java.util.List;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

public class Home extends Activity {

    List<ResolveInfo> mAllApps;
    ListView mAppListView;
    AppListAdapter mAppListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        getAllApp();
        mAppListAdapter = new AppListAdapter(this, mAllApps);
        mAppListView = (ListView) findViewById(R.id.apps);
        mAppListView.setVisibility(View.VISIBLE);
        mAppListView.setAdapter(mAppListAdapter);
        ProgressBar pbar = (ProgressBar) findViewById(R.id.loading);
        pbar.setVisibility(View.GONE);

        // for package add/remove
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package");
        registerReceiver(packageReceiver, filter);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        unregisterReceiver(packageReceiver);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        stopService(new Intent(this, Dragger.class));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getRepeatCount() == 0) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (mIntent != null) {
                    try {
                        startActivity(mIntent);
                        startService(new Intent(Home.this, Dragger.class));
                    } catch(ActivityNotFoundException e) {}
                }
                return true;
            }
        }
        return false;
    }

    Intent mIntent;
    public void onEventMainThread(Intent intent) {
        mIntent = intent;
    }

    PackageManager mPm;
    private void getAllApp() {
        mPm = getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mAllApps = mPm.queryIntentActivities(mainIntent, 0);
        prepareInfo(getComponentName().getPackageName(), true);
        Collections.sort(mAllApps, new StringComparator());// sort by name
    }

    private void prepareInfo(String packageName, boolean loadLabel) {
        for (int i = 0; i < mAllApps.size(); i++) {
            ResolveInfo info = mAllApps.get(i);
            if (info.activityInfo.packageName.equals(packageName)) {
                mAllApps.remove(i);
            } else if (loadLabel) {
                // borrow the dataDir to store label, for loadLabel() is very time consuming
                info.activityInfo.applicationInfo.dataDir = (String) info.loadLabel(mPm);
            }
        }
    }

    BroadcastReceiver packageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String packageName = intent.getDataString().split(":")[1];
            if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
                prepareInfo(packageName, false);
            } else if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
                Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                mainIntent.setPackage(packageName);
                List<ResolveInfo> targetApps = mPm.queryIntentActivities(mainIntent, 0);
                // the new package may not support launcher. so filter it first
                for (int i = 0; i < targetApps.size(); i++) {
                    ResolveInfo info = targetApps.get(i);
                    if (info.activityInfo.packageName.equals(packageName)) {
                        mAllApps.add(info);
                        break;
                    }
                }
            }
            mAppListAdapter.notifyDataSetChanged();
        }
    };

}

@@ -2744,30 +2744,29 @@
 0);%0A        
-p
 re
-par
+mov
 eInfo(getCom
@@ -2798,14 +2798,31 @@
 me()
-, true
+);%0A        prepareInfo(
 );%0A 
@@ -2919,22 +2919,21 @@
 te void 
-p
 re
-par
+mov
 eInfo(St
@@ -2952,27 +2952,8 @@
 Name
-, boolean loadLabel
 ) %7B%0A
@@ -3173,36 +3173,183 @@
     
-%7D else if (loadLabel) %7B%0A    
+    break;%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void prepareInfo() %7B%0A        for (int i = 0; i %3C mAllApps.size(); i++) %7B%0A            ResolveInfo info = mAllApps.get(i);%0A
     
@@ -3445,20 +3445,16 @@
         
-    
 info.act
@@ -3519,30 +3519,16 @@
 l(mPm);%0A
-            %7D%0A
         
@@ -3879,30 +3879,29 @@
             
-p
 re
-par
+mov
 eInfo(packag
@@ -3909,15 +3909,64 @@
 Name
-, false
+);%0A                mAppListAdapter.notifyDataSetChanged(
 );%0A 
@@ -4619,25 +4619,278 @@
     
-mAllApps.add(info
+info.activityInfo.applicationInfo.dataDir = (String) info.loadLabel(mPm);%0A                        mAllApps.add(info);%0A                        Collections.sort(mAllApps, new StringComparator());// sort by name%0A                        mAppListAdapter.notifyDataSetChanged(
 );%0A 
@@ -4977,60 +4977,8 @@
   %7D%0A
-            mAppListAdapter.notifyDataSetChanged();%0A
     

prepare package label when install new app