mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
Avoid exception in rare case where loading of apps is cancelled
git-svn-id: https://zxing.googlecode.com/svn/trunk@1630 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
2874ed0988
commit
7a6c384748
|
@ -17,14 +17,11 @@
|
||||||
package com.google.zxing.client.android.share;
|
package com.google.zxing.client.android.share;
|
||||||
|
|
||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.Browser;
|
import android.provider.Browser;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import com.google.zxing.client.android.R;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -32,12 +29,10 @@ import java.util.List;
|
||||||
public final class AppPickerActivity extends ListActivity {
|
public final class AppPickerActivity extends ListActivity {
|
||||||
|
|
||||||
private final List<String[]> labelsPackages = new ArrayList<String[]>();
|
private final List<String[]> labelsPackages = new ArrayList<String[]>();
|
||||||
private DialogInterface dialog;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle icicle) {
|
protected void onCreate(Bundle icicle) {
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
dialog = ProgressDialog.show(this, "", getString(R.string.msg_loading_apps), true, true);
|
|
||||||
if (labelsPackages.isEmpty()) {
|
if (labelsPackages.isEmpty()) {
|
||||||
new LoadPackagesAsyncTask(this).execute(labelsPackages);
|
new LoadPackagesAsyncTask(this).execute(labelsPackages);
|
||||||
}
|
}
|
||||||
|
@ -59,8 +54,4 @@ public final class AppPickerActivity extends ListActivity {
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
DialogInterface getProgressDialog() {
|
|
||||||
return dialog;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,14 @@
|
||||||
|
|
||||||
package com.google.zxing.client.android.share;
|
package com.google.zxing.client.android.share;
|
||||||
|
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.ListAdapter;
|
import android.widget.ListAdapter;
|
||||||
|
import com.google.zxing.client.android.R;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -46,16 +49,30 @@ final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Void,List<Str
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
private final AppPickerActivity appPickerActivity;
|
private final AppPickerActivity activity;
|
||||||
|
private DialogInterface dialog;
|
||||||
|
|
||||||
LoadPackagesAsyncTask(AppPickerActivity appPickerActivity) {
|
LoadPackagesAsyncTask(AppPickerActivity activity) {
|
||||||
this.appPickerActivity = appPickerActivity;
|
this.activity = activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected synchronized void onPreExecute() {
|
||||||
|
dialog = ProgressDialog.show(activity, "", activity.getString(R.string.msg_loading_apps), true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected synchronized void onCancelled() {
|
||||||
|
if (dialog != null) {
|
||||||
|
dialog.dismiss();
|
||||||
|
dialog = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<String[]> doInBackground(List<String[]>... objects) {
|
protected List<String[]> doInBackground(List<String[]>... objects) {
|
||||||
List<String[]> labelsPackages = objects[0];
|
List<String[]> labelsPackages = objects[0];
|
||||||
PackageManager packageManager = appPickerActivity.getPackageManager();
|
PackageManager packageManager = activity.getPackageManager();
|
||||||
List<ApplicationInfo> appInfos = packageManager.getInstalledApplications(0);
|
List<ApplicationInfo> appInfos = packageManager.getInstalledApplications(0);
|
||||||
for (ApplicationInfo appInfo : appInfos) {
|
for (ApplicationInfo appInfo : appInfos) {
|
||||||
CharSequence label = appInfo.loadLabel(packageManager);
|
CharSequence label = appInfo.loadLabel(packageManager);
|
||||||
|
@ -88,15 +105,18 @@ final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Void,List<Str
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(List<String[]> results) {
|
protected synchronized void onPostExecute(List<String[]> results) {
|
||||||
List<String> labels = new ArrayList<String>(results.size());
|
List<String> labels = new ArrayList<String>(results.size());
|
||||||
for (String[] result : results) {
|
for (String[] result : results) {
|
||||||
labels.add(result[0]);
|
labels.add(result[0]);
|
||||||
}
|
}
|
||||||
ListAdapter listAdapter = new ArrayAdapter<String>(
|
ListAdapter listAdapter = new ArrayAdapter<String>(
|
||||||
appPickerActivity, android.R.layout.simple_list_item_1, labels);
|
activity, android.R.layout.simple_list_item_1, labels);
|
||||||
appPickerActivity.setListAdapter(listAdapter);
|
activity.setListAdapter(listAdapter);
|
||||||
appPickerActivity.getProgressDialog().dismiss();
|
if (dialog != null) {
|
||||||
|
dialog.dismiss();
|
||||||
|
dialog = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ByFirstStringComparator implements Comparator<String[]>, Serializable {
|
private static class ByFirstStringComparator implements Comparator<String[]>, Serializable {
|
||||||
|
|
Loading…
Reference in a new issue