mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Add icon to app picker
git-svn-id: https://zxing.googlecode.com/svn/trunk@2601 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
b8873b8007
commit
496399e9b5
21
android/res/layout/app_picker_list_item.xml
Normal file
21
android/res/layout/app_picker_list_item.xml
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<ImageView android:id="@+id/app_picker_list_item_icon"
|
||||||
|
android:layout_width="64dip"
|
||||||
|
android:layout_height="64dip"
|
||||||
|
android:scaleType="centerInside"
|
||||||
|
android:padding="@dimen/half_padding"/>
|
||||||
|
|
||||||
|
<TextView android:id="@+id/app_picker_list_item_label"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:padding="@dimen/half_padding"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
|
@ -24,11 +24,13 @@
|
||||||
<TextView android:id="@+id/bookmark_title"
|
<TextView android:id="@+id/bookmark_title"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||||
android:singleLine="true"/>
|
android:singleLine="true"/>
|
||||||
|
|
||||||
<TextView android:id="@+id/bookmark_url"
|
<TextView android:id="@+id/bookmark_url"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||||
android:singleLine="false"/>
|
android:singleLine="false"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013 ZXing authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.android.share;
|
||||||
|
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
|
||||||
|
final class AppInfo implements Comparable<AppInfo> {
|
||||||
|
|
||||||
|
private final String packageName;
|
||||||
|
private final String label;
|
||||||
|
private final Drawable icon;
|
||||||
|
|
||||||
|
AppInfo(String packageName, String label, Drawable icon) {
|
||||||
|
this.packageName = packageName;
|
||||||
|
this.label = label;
|
||||||
|
this.icon = icon;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getPackageName() {
|
||||||
|
return packageName;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getLabel() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
Drawable getIcon() {
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(AppInfo another) {
|
||||||
|
return label.compareTo(another.label);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return label.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (!(other instanceof AppInfo)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
AppInfo another = (AppInfo) other;
|
||||||
|
return label.equals(another.label);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -26,16 +26,17 @@ import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
|
||||||
import com.google.zxing.client.android.common.executor.AsyncTaskExecManager;
|
import com.google.zxing.client.android.common.executor.AsyncTaskExecManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public final class AppPickerActivity extends ListActivity {
|
public final class AppPickerActivity extends ListActivity {
|
||||||
|
|
||||||
private final List<String[]> labelsPackages;
|
private final List<AppInfo> labelsPackages;
|
||||||
private LoadPackagesAsyncTask backgroundTask;
|
private LoadPackagesAsyncTask backgroundTask;
|
||||||
private final AsyncTaskExecInterface taskExec;
|
private final AsyncTaskExecInterface taskExec;
|
||||||
|
|
||||||
public AppPickerActivity() {
|
public AppPickerActivity() {
|
||||||
labelsPackages = new ArrayList<String[]>();
|
labelsPackages = Collections.synchronizedList(new ArrayList<AppInfo>());
|
||||||
taskExec = new AsyncTaskExecManager().build();
|
taskExec = new AsyncTaskExecManager().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,13 +61,13 @@ public final class AppPickerActivity extends ListActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onListItemClick(ListView l, View view, int position, long id) {
|
protected void onListItemClick(ListView l, View view, int position, long id) {
|
||||||
if (position >= 0 && position < labelsPackages.size()) {
|
if (position >= 0 && position < labelsPackages.size()) {
|
||||||
String url = "market://details?id=" + labelsPackages.get(position)[1];
|
String packageName = labelsPackages.get(position).getPackageName();
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
||||||
intent.putExtra(Browser.BookmarkColumns.URL, url);
|
intent.putExtra(Browser.BookmarkColumns.URL, "market://details?id=" + packageName);
|
||||||
setResult(RESULT_OK, intent);
|
setResult(RESULT_OK, intent);
|
||||||
} else {
|
} else {
|
||||||
setResult(RESULT_CANCELED);
|
setResult(RESULT_CANCELED);
|
||||||
}
|
}
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,16 +16,19 @@
|
||||||
|
|
||||||
package com.google.zxing.client.android.share;
|
package com.google.zxing.client.android.share;
|
||||||
|
|
||||||
|
import android.app.ListActivity;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.ListAdapter;
|
import android.widget.ListAdapter;
|
||||||
|
import com.google.zxing.client.android.R;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +36,7 @@ import java.util.List;
|
||||||
*
|
*
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Object,List<String[]>> {
|
final class LoadPackagesAsyncTask extends AsyncTask<List<AppInfo>,Object,List<AppInfo>> {
|
||||||
|
|
||||||
private static final String[] PKG_PREFIX_WHITELIST = {
|
private static final String[] PKG_PREFIX_WHITELIST = {
|
||||||
"com.google.android.apps.",
|
"com.google.android.apps.",
|
||||||
|
@ -45,27 +48,30 @@ final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Object,List<S
|
||||||
"com.htc",
|
"com.htc",
|
||||||
};
|
};
|
||||||
|
|
||||||
private final AppPickerActivity activity;
|
private final ListActivity activity;
|
||||||
|
|
||||||
LoadPackagesAsyncTask(AppPickerActivity activity) {
|
LoadPackagesAsyncTask(ListActivity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<String[]> doInBackground(List<String[]>... objects) {
|
protected List<AppInfo> doInBackground(List<AppInfo>... objects) {
|
||||||
List<String[]> labelsPackages = objects[0];
|
List<AppInfo> labelsPackages = objects[0];
|
||||||
PackageManager packageManager = activity.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);
|
String packageName = appInfo.packageName;
|
||||||
if (label != null) {
|
if (!isHidden(packageName)) {
|
||||||
String packageName = appInfo.packageName;
|
CharSequence label = appInfo.loadLabel(packageManager);
|
||||||
if (!isHidden(packageName)) {
|
Drawable icon = appInfo.loadIcon(packageManager);
|
||||||
labelsPackages.add(new String[]{label.toString(), packageName});
|
if (label != null) {
|
||||||
|
labelsPackages.add(new AppInfo(packageName, label.toString(), icon));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Collections.sort(labelsPackages, new ByFirstStringComparator());
|
synchronized (labelsPackages) {
|
||||||
|
Collections.sort(labelsPackages);
|
||||||
|
}
|
||||||
return labelsPackages;
|
return labelsPackages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,21 +93,22 @@ final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Object,List<S
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected synchronized void onPostExecute(List<String[]> results) {
|
protected void onPostExecute(final List<AppInfo> results) {
|
||||||
List<String> labels = new ArrayList<String>(results.size());
|
ListAdapter listAdapter = new ArrayAdapter<AppInfo>(activity,
|
||||||
for (String[] result : results) {
|
R.layout.app_picker_list_item,
|
||||||
labels.add(result[0]);
|
R.id.app_picker_list_item_label,
|
||||||
}
|
results) {
|
||||||
ListAdapter listAdapter = new ArrayAdapter<String>(activity,
|
@Override
|
||||||
android.R.layout.simple_list_item_1, labels);
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
|
View view = super.getView(position, convertView, parent);
|
||||||
|
Drawable icon = results.get(position).getIcon();
|
||||||
|
if (icon != null) {
|
||||||
|
((ImageView) view.findViewById(R.id.app_picker_list_item_icon)).setImageDrawable(icon);
|
||||||
|
}
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
};
|
||||||
activity.setListAdapter(listAdapter);
|
activity.setListAdapter(listAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class ByFirstStringComparator implements Comparator<String[]>, Serializable {
|
|
||||||
@Override
|
|
||||||
public int compare(String[] o1, String[] o2) {
|
|
||||||
return o1[0].compareTo(o2[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue