// Path: app/src/main/java/org/mars3142/android/toaster/data/PackageEntry.java
// public class PackageEntry {
// 
//     private ApplicationInfo mInfo;
// 
//     private File mApkFile;
//     private String mLabel;
//     private Drawable mIcon;
//     private Boolean mMounted;
// 
//     public PackageEntry(ApplicationInfo applicationInfo) {
//         mInfo = applicationInfo;
//         mApkFile = new File(mInfo.sourceDir);
//     }
// 
//     public String getPackageName() {
//         if (mInfo != null) {
//             return mInfo.packageName;
//         }
// 
//         return null;
//     }
// 
//     public String getLabel(Context context) {
//         if (mLabel == null || !mMounted) {
//             if (!mApkFile.exists()) {
//                 mMounted = false;
//                 mLabel = mInfo.packageName;
//             } else {
//                 mMounted = true;
//                 CharSequence label = mInfo.loadLabel(context.getPackageManager());
//                 mLabel = label != null ? label.toString() : mInfo.packageName;
//             }
//         }
// 
//         return mLabel;
//     }
// 
//     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
//     public Drawable getIcon(Context context) {
//         if (mIcon == null) {
//             if (mApkFile.exists()) {
//                 mIcon = mInfo.loadIcon(context.getPackageManager());
//                 return mIcon;
//             } else {
//                 mMounted = false;
//             }
//         } else if (!mMounted) {
//             if (mApkFile.exists()) {
//                 mMounted = true;
//                 mIcon = mInfo.loadIcon(context.getPackageManager());
//                 return mIcon;
//             }
//         } else {
//             return mIcon;
//         }
// 
//         Drawable drawable;
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//             drawable = context.getDrawable(android.R.drawable.sym_def_app_icon);
//         } else {
//             drawable = context.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
//         }
//         return drawable;
//     }
// 
//     @Override
//     public String toString() {
//         return mLabel;
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/observer/InstalledAppsObserver.java
// public class InstalledAppsObserver extends BroadcastReceiver {
// 
//     private PackageLoader mLoader;
// 
//     public InstalledAppsObserver(PackageLoader loader) {
//         mLoader = loader;
// 
//         IntentFilter filter = new IntentFilter();
//         filter.addAction(Intent.ACTION_PACKAGE_ADDED);
//         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
//         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
//         filter.addDataScheme("package");
//         mLoader.getContext().registerReceiver(this, filter);
// 
//         IntentFilter sdFilter = new IntentFilter();
//         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
//         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
//         mLoader.getContext().registerReceiver(this, sdFilter);
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         mLoader.onContentChanged();
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/observer/SystemLocalObserver.java
// public class SystemLocalObserver extends BroadcastReceiver {
// 
//     private PackageLoader mLoader;
// 
//     public SystemLocalObserver(PackageLoader loader) {
//         mLoader = loader;
// 
//         IntentFilter filter = new IntentFilter();
//         filter.addAction(Intent.ACTION_LOCALE_CHANGED);
//         mLoader.getContext().registerReceiver(this, filter);
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         mLoader.onContentChanged();
//     }
// }


import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import org.mars3142.android.toaster.data.PackageEntry;
import org.mars3142.android.toaster.observer.InstalledAppsObserver;
import org.mars3142.android.toaster.observer.SystemLocalObserver;
import java.util.ArrayList;
/*
 * This file is part of Toaster
 *
 * Copyright (c) 2014, 2017 Peter Siegmund
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mars3142.android.toaster.loader;




/**
 * @author mars3142
 *
 * inspired by https://github.com/alexjlockwood/AppListLoader/blob/master/src/com/adp/loadercustom/loader/AppListLoader.java
 */
public class PackageLoader extends AsyncTaskLoader<PackageEntry[]> {

    /**
     * Key for source param
     */
    public static final String SOURCE = "source";

    /**
     * Parameter SOURCE is for apps
     */
    public static final int SOURCE_APPS = 0;

    /**
     * Parameter SOURCE is for database
     */
    public static final int SOURCE_DATABASE = 1;

    private int mSource;
    private PackageManager mManager;
    private InstalledAppsObserver mAppsObserver;
// Path: app/src/main/java/org/mars3142/android/toaster/data/PackageEntry.java
// public class PackageEntry {
// 
//     private ApplicationInfo mInfo;
// 
//     private File mApkFile;
//     private String mLabel;
//     private Drawable mIcon;
//     private Boolean mMounted;
// 
//     public PackageEntry(ApplicationInfo applicationInfo) {
//         mInfo = applicationInfo;
//         mApkFile = new File(mInfo.sourceDir);
//     }
// 
//     public String getPackageName() {
//         if (mInfo != null) {
//             return mInfo.packageName;
//         }
// 
//         return null;
//     }
// 
//     public String getLabel(Context context) {
//         if (mLabel == null || !mMounted) {
//             if (!mApkFile.exists()) {
//                 mMounted = false;
//                 mLabel = mInfo.packageName;
//             } else {
//                 mMounted = true;
//                 CharSequence label = mInfo.loadLabel(context.getPackageManager());
//                 mLabel = label != null ? label.toString() : mInfo.packageName;
//             }
//         }
// 
//         return mLabel;
//     }
// 
//     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
//     public Drawable getIcon(Context context) {
//         if (mIcon == null) {
//             if (mApkFile.exists()) {
//                 mIcon = mInfo.loadIcon(context.getPackageManager());
//                 return mIcon;
//             } else {
//                 mMounted = false;
//             }
//         } else if (!mMounted) {
//             if (mApkFile.exists()) {
//                 mMounted = true;
//                 mIcon = mInfo.loadIcon(context.getPackageManager());
//                 return mIcon;
//             }
//         } else {
//             return mIcon;
//         }
// 
//         Drawable drawable;
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//             drawable = context.getDrawable(android.R.drawable.sym_def_app_icon);
//         } else {
//             drawable = context.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
//         }
//         return drawable;
//     }
// 
//     @Override
//     public String toString() {
//         return mLabel;
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/observer/InstalledAppsObserver.java
// public class InstalledAppsObserver extends BroadcastReceiver {
// 
//     private PackageLoader mLoader;
// 
//     public InstalledAppsObserver(PackageLoader loader) {
//         mLoader = loader;
// 
//         IntentFilter filter = new IntentFilter();
//         filter.addAction(Intent.ACTION_PACKAGE_ADDED);
//         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
//         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
//         filter.addDataScheme("package");
//         mLoader.getContext().registerReceiver(this, filter);
// 
//         IntentFilter sdFilter = new IntentFilter();
//         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
//         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
//         mLoader.getContext().registerReceiver(this, sdFilter);
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         mLoader.onContentChanged();
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/observer/SystemLocalObserver.java
// public class SystemLocalObserver extends BroadcastReceiver {
// 
//     private PackageLoader mLoader;
// 
//     public SystemLocalObserver(PackageLoader loader) {
//         mLoader = loader;
// 
//         IntentFilter filter = new IntentFilter();
//         filter.addAction(Intent.ACTION_LOCALE_CHANGED);
//         mLoader.getContext().registerReceiver(this, filter);
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         mLoader.onContentChanged();
//     }
// }


// Path: app/src/main/java/org/mars3142/android/toaster/loader/PackageLoader.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import org.mars3142.android.toaster.data.PackageEntry;
import org.mars3142.android.toaster.observer.InstalledAppsObserver;
import org.mars3142.android.toaster.observer.SystemLocalObserver;
import java.util.ArrayList;

/*
 * This file is part of Toaster
 *
 * Copyright (c) 2014, 2017 Peter Siegmund
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mars3142.android.toaster.loader;




/**
 * @author mars3142
 *
 * inspired by https://github.com/alexjlockwood/AppListLoader/blob/master/src/com/adp/loadercustom/loader/AppListLoader.java
 */
public class PackageLoader extends AsyncTaskLoader<PackageEntry[]> {

    /**
     * Key for source param
     */
    public static final String SOURCE = "source";

    /**
     * Parameter SOURCE is for apps
     */
    public static final int SOURCE_APPS = 0;

    /**
     * Parameter SOURCE is for database
     */
    public static final int SOURCE_DATABASE = 1;

    private int mSource;
    private PackageManager mManager;
    private InstalledAppsObserver mAppsObserver;
    private SystemLocalObserver mLocalObserver;