|
@@ -1,105 +1,90 @@
|
|
|
package org.h4e.utils.iconimport.utils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
|
|
|
+import org.h4e.utils.iconimport.utils.objects.AppDetails;
|
|
|
+
|
|
|
import android.app.AlertDialog;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
-import android.content.Intent;
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
import android.content.pm.PackageManager;
|
|
|
-import android.graphics.Bitmap;
|
|
|
-import android.graphics.drawable.BitmapDrawable;
|
|
|
-import android.graphics.drawable.Drawable;
|
|
|
|
|
|
public class AppChooser {
|
|
|
|
|
|
- private List<Intent> mIntentList = null;
|
|
|
- private List<String> mPackageList = null;
|
|
|
- private List<String> mLabelList = null;
|
|
|
- private List<Drawable> mIconList = null;
|
|
|
-
|
|
|
- private AlertDialog mAlertDialog = null;
|
|
|
-
|
|
|
- public interface AppSelectedListener {
|
|
|
+ public interface OnAppSelectedListener {
|
|
|
|
|
|
- public void appSelected(Intent appIntent, String appLabel,
|
|
|
- String appPackage, Bitmap appIcon);
|
|
|
+ public void onAppSelected(AppDetails details);
|
|
|
}
|
|
|
|
|
|
- private AppSelectedListener mAppSelectedListener;
|
|
|
+ private Context mContext = null;
|
|
|
+ private PackageManager mPackageManager = null;
|
|
|
+ private List<String> mLabelList = null;
|
|
|
+ private OnAppSelectedListener mOnAppSelectedListener = null;
|
|
|
+ private AlertDialog mAlertDialog = null;
|
|
|
|
|
|
- public AppChooser(Context context, AppSelectedListener appSelectedListener) {
|
|
|
+ public AppChooser(Context context,
|
|
|
+ OnAppSelectedListener onAppSelectedListener) {
|
|
|
|
|
|
- mAppSelectedListener = appSelectedListener;
|
|
|
+ mContext = context;
|
|
|
+ mOnAppSelectedListener = onAppSelectedListener;
|
|
|
+ mLabelList = new ArrayList<String>();
|
|
|
|
|
|
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
|
|
|
- dialogBuilder.setTitle("Choose an App");
|
|
|
+ loadAppList();
|
|
|
+ buildDialog();
|
|
|
+ }
|
|
|
|
|
|
- final PackageManager packageManager = context.getPackageManager();
|
|
|
+ public void show() {
|
|
|
|
|
|
- List<ApplicationInfo> packageList = packageManager
|
|
|
- .getInstalledApplications(PackageManager.GET_META_DATA);
|
|
|
+ mAlertDialog.show();
|
|
|
+ }
|
|
|
|
|
|
- if (!packageList.isEmpty()) {
|
|
|
+ private void loadAppList() {
|
|
|
|
|
|
- mIntentList = new ArrayList<Intent>();
|
|
|
- mPackageList = new ArrayList<String>();
|
|
|
- mLabelList = new ArrayList<String>();
|
|
|
- mIconList = new ArrayList<Drawable>();
|
|
|
+ mPackageManager = mContext.getPackageManager();
|
|
|
+ List<ApplicationInfo> packageList = mPackageManager
|
|
|
+ .getInstalledApplications(PackageManager.GET_META_DATA);
|
|
|
|
|
|
- for (int x = 0; x < packageList.size(); x++) {
|
|
|
+ if (packageList.isEmpty())
|
|
|
+ return;
|
|
|
|
|
|
- ApplicationInfo appInfo = packageList.get(x);
|
|
|
- Intent i = packageManager
|
|
|
- .getLaunchIntentForPackage(appInfo.packageName);
|
|
|
+ for (int x = 0; x < packageList.size(); x++) {
|
|
|
|
|
|
- if (i != null) {
|
|
|
+ ApplicationInfo appInfo = packageList.get(x);
|
|
|
|
|
|
- mIntentList.add(i);
|
|
|
+ if (mPackageManager.getLaunchIntentForPackage(appInfo.packageName) != null) {
|
|
|
|
|
|
- mPackageList.add(appInfo.packageName);
|
|
|
- mLabelList.add(appInfo.loadLabel(
|
|
|
- context.getPackageManager()).toString());
|
|
|
- mIconList
|
|
|
- .add(appInfo.loadIcon(context.getPackageManager()));
|
|
|
- }
|
|
|
+ mLabelList.add(packageList.get(x).loadLabel(mPackageManager)
|
|
|
+ .toString());
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- String[] labelArray = mLabelList.toArray(new String[mLabelList
|
|
|
- .size()]);
|
|
|
-
|
|
|
- dialogBuilder.setItems(labelArray,
|
|
|
- new DialogInterface.OnClickListener() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(DialogInterface dialog, int which) {
|
|
|
-
|
|
|
- Bitmap appIcon = ((BitmapDrawable) mIconList
|
|
|
- .get(which)).getBitmap();
|
|
|
+ Collections.sort(mLabelList);
|
|
|
+ }
|
|
|
|
|
|
- // TODO Ensure bitmap is proper size, if larger,
|
|
|
- // scale it.
|
|
|
+ private void buildDialog() {
|
|
|
|
|
|
- mAppSelectedListener.appSelected(
|
|
|
- mIntentList.get(which),
|
|
|
- mLabelList.get(which),
|
|
|
- mPackageList.get(which), appIcon);
|
|
|
- }
|
|
|
- });
|
|
|
+ String[] labelArray = mLabelList.toArray(new String[mLabelList.size()]);
|
|
|
|
|
|
- mAlertDialog = dialogBuilder.create();
|
|
|
+ AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
|
|
|
|
|
|
- } else {
|
|
|
+ dialogBuilder.setTitle("Choose an app");
|
|
|
+ dialogBuilder.setItems(labelArray, mOnDialogClickListener);
|
|
|
|
|
|
- throw new RuntimeException("");
|
|
|
- }
|
|
|
+ mAlertDialog = dialogBuilder.create();
|
|
|
}
|
|
|
|
|
|
- public void show() {
|
|
|
+ private DialogInterface.OnClickListener mOnDialogClickListener = new DialogInterface.OnClickListener() {
|
|
|
|
|
|
- mAlertDialog.show();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+
|
|
|
+ AppDetails details = AppDetails.getDetails(mPackageManager,
|
|
|
+ mLabelList.get(which));
|
|
|
|
|
|
+ mOnAppSelectedListener.onAppSelected(details);
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|