|
Android example source code file (LauncherActivity.java)
The LauncherActivity.java Android example source code
/*
* Copyright (C) 2007 The Android Open Source Project
*
* 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 android.app;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Displays a list of all activities which can be performed
* for a given intent. Launches when clicked.
*
*/
public abstract class LauncherActivity extends ListActivity {
Intent mIntent;
PackageManager mPackageManager;
IconResizer mIconResizer;
/**
* An item in the list
*/
public static class ListItem {
public ResolveInfo resolveInfo;
public CharSequence label;
public Drawable icon;
public String packageName;
public String className;
public Bundle extras;
ListItem(PackageManager pm, ResolveInfo resolveInfo, IconResizer resizer) {
this.resolveInfo = resolveInfo;
label = resolveInfo.loadLabel(pm);
ComponentInfo ci = resolveInfo.activityInfo;
if (ci == null) ci = resolveInfo.serviceInfo;
if (label == null && ci != null) {
label = resolveInfo.activityInfo.name;
}
if (resizer != null) {
icon = resizer.createIconThumbnail(resolveInfo.loadIcon(pm));
}
packageName = ci.applicationInfo.packageName;
className = ci.name;
}
public ListItem() {
}
}
/**
* Adapter which shows the set of activities that can be performed for a given intent.
*/
private class ActivityAdapter extends BaseAdapter implements Filterable {
private final Object lock = new Object();
private ArrayList<ListItem> mOriginalValues;
protected final IconResizer mIconResizer;
protected final LayoutInflater mInflater;
protected List<ListItem> mActivitiesList;
private Filter mFilter;
public ActivityAdapter(IconResizer resizer) {
mIconResizer = resizer;
mInflater = (LayoutInflater) LauncherActivity.this.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mActivitiesList = makeListItems();
}
public Intent intentForPosition(int position) {
if (mActivitiesList == null) {
return null;
}
Intent intent = new Intent(mIntent);
ListItem item = mActivitiesList.get(position);
intent.setClassName(item.packageName, item.className);
if (item.extras != null) {
intent.putExtras(item.extras);
}
return intent;
}
public ListItem itemForPosition(int position) {
if (mActivitiesList == null) {
return null;
}
return mActivitiesList.get(position);
}
public int getCount() {
return mActivitiesList != null ? mActivitiesList.size() : 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(
com.android.internal.R.layout.activity_list_item_2, parent, false);
} else {
view = convertView;
}
bindView(view, mActivitiesList.get(position));
return view;
}
private void bindView(View view, ListItem item) {
TextView text = (TextView) view;
text.setText(item.label);
if (item.icon == null) {
item.icon = mIconResizer.createIconThumbnail(
item.resolveInfo.loadIcon(getPackageManager()));
}
text.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
/**
* An array filters constrains the content of the array adapter with a prefix. Each
* item that does not start with the supplied prefix is removed from the list.
*/
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<ListItem>(mActivitiesList);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<ListItem> list = new ArrayList
Other Android examples (source code examples)Here is a short list of links related to this Android LauncherActivity.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.