Skip to content

Instantly share code, notes, and snippets.

@FeherMarcell
Created October 1, 2012 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FeherMarcell/3812803 to your computer and use it in GitHub Desktop.
Save FeherMarcell/3812803 to your computer and use it in GitHub Desktop.
AndroidLabor4HomeScreen
class ApplicationInfo {
/**
* The application name.
*/
CharSequence title;
/**
* The intent used to start the application.
*/
Intent intent;
/**
* The application icon.
*/
Drawable icon;
/**
* When set to true, indicates that the icon has been resized.
*/
boolean filtered;
/**
* Creates the application intent based on a component name and various launch flags.
*
* @param className the class name of the component representing the intent
* @param launchFlags the launch flags
*/
final void setActivity(ComponentName className, int launchFlags) {
intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(className);
intent.setFlags(launchFlags);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ApplicationInfo)) {
return false;
}
ApplicationInfo that = (ApplicationInfo) o;
return title.equals(that.title) &&
intent.getComponent().getClassName().equals(
that.intent.getComponent().getClassName());
}
@Override
public int hashCode() {
int result;
result = (title != null ? title.hashCode() : 0);
final String name = intent.getComponent().getClassName();
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- All applications on the top side of the screen -->
<GridView
android:id="@+id/all_apps"
android:persistentDrawingCache="animation|scrolling"
android:alwaysDrawnWithCache="true"
android:scrollbars="none"
android:drawSelectorOnTop="false"
android:numColumns="auto_fit"
android:columnWidth="78dp"
android:stretchMode="spacingWidth"
android:layout_weight="1.0"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:stackFromBottom="true"
android:visibility="visible" />
</LinearLayout>
private void loadApplications(){
// PackageManager referencia
PackageManager manager = getPackageManager();
// listat keszitunk az osszes megjelenitendo alkalmazasrol
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
// rendezzuk nev szerint
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
// feltoltjuk az ApplicationInfo tombot minden megjelenitendo alkalamzashoz
if (apps != null) {
final int count = apps.size();
if (mApplications == null) {
mApplications = new ArrayList<ApplicationInfo>(count);
}
mApplications.clear();
for (int i = 0; i < count; i++) {
ApplicationInfo application = new ApplicationInfo();
ResolveInfo info = apps.get(i);
// alkalmazas neve
application.title = info.loadLabel(manager);
// ahhoz hogy kattintasra el tudjuk inditani, kelleni fog egy megfelelo Activity-re mutato Intent
application.setActivity(new ComponentName(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name),
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// ikon
application.icon = info.activityInfo.loadIcon(manager);
mApplications.add(application);
}
}
}
/**
* Egyedi grid adatper az ApplicationInfo tombhoz
*/
private class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> {
private Rect mOldBounds = new Rect();
public ApplicationsAdapter(Context context, ArrayList<ApplicationInfo> apps) {
super(context, 0, apps);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ApplicationInfo info = mApplications.get(position);
// ez egy trukk a meglevo convertView objektum ujrahasznositasahoz
// sokat javit a sebessegen, erdemes hasznalni!
if (convertView == null) {
final LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.application, parent, false);
}
Drawable icon = info.icon;
if (!info.filtered) {
final Resources resources = getContext().getResources();
int width = (int) resources.getDimension(android.R.dimen.app_icon_size);
int height = (int) resources.getDimension(android.R.dimen.app_icon_size);
final int iconWidth = icon.getIntrinsicWidth();
final int iconHeight = icon.getIntrinsicHeight();
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
}
if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) {
final float ratio = (float) iconWidth / iconHeight;
if (iconWidth > iconHeight) {
height = (int) (width / ratio);
} else if (iconHeight > iconWidth) {
width = (int) (height * ratio);
}
final Bitmap.Config c =
icon.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
final Bitmap thumb = Bitmap.createBitmap(width, height, c);
final Canvas canvas = new Canvas(thumb);
canvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, 0));
// Copy the old bounds to restore them later
// If we were to do oldBounds = icon.getBounds(),
// the call to setBounds() that follows would
// change the same instance and we would lose the
// old bounds
mOldBounds.set(icon.getBounds());
icon.setBounds(0, 0, width, height);
icon.draw(canvas);
icon.setBounds(mOldBounds);
icon = info.icon = new BitmapDrawable(thumb);
info.filtered = true;
}
}
final TextView textView = (TextView) convertView.findViewById(R.id.label);
textView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
textView.setText(info.title);
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment