Archive

Posts Tagged ‘launcher’

Launch Home Chooser Dialog


After long time digging in Android source code, I’ve found a way to pop-up the Home Chooser dialog.

Home Chooser Dialog

Home Chooser Dialog

Above is how it displays on my phone. Anyway, here the way how to do it:

    static final String HOME_CHOOSER_PACKAGE_NAME = "android";
    static final String HOME_CHOOSER_CLASS_NAME = "com.android.internal.app.ResolverActivity";

    private void launchHomeChooser() {
    	Intent i = new Intent(Intent.ACTION_MAIN);
    	i.addCategory(Intent.CATEGORY_HOME);
    	i.setClassName(HOME_CHOOSER_PACKAGE_NAME, HOME_CHOOSER_CLASS_NAME);
    	startActivity(i);
    }

If you search over Internet, there might be many other ways to create a similar Home Chooser dialog, however, it takes lots of coding and customization. Well, I prefer to call the framework one.

One more thing, it works on my device Galaxy SII, running ROM Resurrection Remix v2.5.3 (Android ICS v4.0.4); so I’m not really certain that it would work for every device, but it should work for most of devices then. It runs fine on Android 2.3.x devices, too.

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , ,

Get Default Launcher on Device


A simple snippet to get default launcher on device, which is checked “Use by default for this action” on Launcher selection dialog.

	public static ApplicationInfo getDefaultLauncher(Context context) {
		// first query all installed launcher
		ArrayList<ApplicationInfo> listAppInfo = getAllLaunchers(context);

		// the default launcher queried being stored here
		ArrayList<ComponentName> listDefault = new ArrayList<ComponentName>();
		// create list IntentFilter for Launcher apps
		final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
		filter.addCategory(Intent.CATEGORY_HOME);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		ArrayList<IntentFilter> listFilters = new ArrayList<IntentFilter>();
		listFilters.add(filter);
		// query default launcher
		context.getPackageManager().getPreferredActivities(listFilters, listDefault, null);

		// check if default launcher
		if(listDefault.size() > 0) {
			for(ComponentName name: listDefault) {
				for(ApplicationInfo info: listAppInfo) {
					if(name.getPackageName().equals(info.packageName)) {
						// found yeah!
						return info;
					}
				}
			}
		}

		// not found any, no launcher set Default
		return null;
	}

Refer to my previous post to get all install Launchers on device.

 

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: ,

Query all installed Launcher Applications

July 3, 2012 1 comment

The tip for making this happen is that all launchers register these two categories in Manifest

+ CATEGORY_HOME

+ CATEGORY_DEFAULT

and this is my snippet to query ’em all.

	public static ArrayList<ApplicationInfo> getAllLaunchers(Context context) {
		// create new intent
		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		// all launchers register these two categories
		mainIntent.addCategory(Intent.CATEGORY_HOME);
		mainIntent.addCategory(Intent.CATEGORY_DEFAULT);

		// query normally
		final ArrayList<ResolveInfo> pkgAppsList = (ArrayList<ResolveInfo>)
				context.getPackageManager().queryIntentActivities( mainIntent, 0);

		// you can return 'pkgAppsList'
		// anyway, I need the following information,
		// so I just query what I need
		ArrayList<ApplicationInfo> listAppInfo = new ArrayList<ApplicationInfo>();
		for(ResolveInfo info: pkgAppsList) {
			listAppInfo.add(info.activityInfo.applicationInfo);
		}

		// return
		return listAppInfo;
	}

There you go, gotta do something with it.
Enjoy and have fun!

Cheers,
Pete Houston

Categories: Tricks & Tips Tags: , , , ,

Create Application Launcher as a list

June 12, 2011 39 comments

This is how Google Android default Application screen looks like:

Default Android Application Launcher

Default Android Application Launcher

It’s a grid view and when you click on one of those, the corresponding activity is launched.

Ok, so now I’ll guide you how to create a similar one but in a list view. Here the screenshot:

Application Viewer

Application Viewer

A – What Does It Do?

– All the installed application on the machine is displayed with their icons, label names and package names.

– When an item is clicked the corresponding activity is launched.

– If the item does not exist, a small message notification will display. (it might not happen, lol)

– If the item exist, but current user does not have the permission to launch, well, just do nothing. (it’s kinda fun on slow emulator :D)

B – Create the Project

Project Name: Application Viewer

Build Target: Android 2.3.3 (the latest one at this time, well, I like it though)

Application Name: Application Viewer

Package Name: pete.android.study

Create Activity: MainActivity

Min SDK: 10

C – Sketch the Layout

Like old times (my previous post), we need two layouts:

+ one layout for each item in the list view

+ one layout for main list view display

1. List Item Layout (/res/layout/layout_appinfo.xml)

– You may want to refer to my previous tutorial on creating a PhoneBook, because the layout is much alike. I’m feeling lazy to describe it again…zzz

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:padding="5dip"  
  >
  
  <ImageView
      android:id="@+id/ivIcon"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_marginRight="5dip"
      android:scaleType="center"
  />
  
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="0dip"
      android:layout_height="fill_parent" 
      android:layout_weight="1"     
  >
  
      <TextView
          android:id="@+id/tvName"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="1"
          android:gravity="center_vertical"          
      />
      
      <TextView
          android:id="@+id/tvPack"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="1"
          android:singleLine="true"
          android:ellipsize="marquee"          
      />
  
  </LinearLayout>
  
  
</LinearLayout>

2. Main Layout (/res/layout/layout_main.xml)

– Just one single list view to apply.

<?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"
    >

	<ListView
		android:id="@+id/lvApps"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"		
	/>

</LinearLayout>

D – Class Design: On the Idea

This is a quite complicated shit of class diagram. I’m just thinking of making it more details.

Application Viewer - Class Diagram

Application Viewer - Class Diagram

MainActivity: the entry of the program, we will load list view here, create and set adapter to list view, and handle OnItemClickListener (which is from AdapterView) in list view.

AppInfoAdapter: the adapter inherits from BaseAdapter to handle list item, we need to handle things in getView() (loading controls from resources, set them appropriate values,  get application info meta data..).

Utilities: to support

+ get installed application list.

+ launch an activity.

E – From Design to Coding (w/ Passion)

1. AppInfoAdapter: (AppInfoAdapter.java)

package pete.android.study;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter {
	private Context mContext;
	private List mListAppInfo;
	private PackageManager mPackManager;

	public AppInfoAdapter(Context c, List list, PackageManager pm) {
		mContext = c;
		mListAppInfo = list;
		mPackManager = pm;
	}

	@Override
	public int getCount() {
		return mListAppInfo.size();
	}

	@Override
	public Object getItem(int position) {
		return mListAppInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// get the selected entry
		ApplicationInfo entry = mListAppInfo.get(position);

		// reference to convertView
		View v = convertView;

		// inflate new layout if null
		if(v == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.layout_appinfo, null);
		}

		// load controls from layout resources
		ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
		TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
		TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

		// set data to display
		ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
		tvAppName.setText(entry.loadLabel(mPackManager));
		tvPkgName.setText(entry.packageName);

		// return view
		return v;
	}
}

2. Utilities: (Utilities.java)

package pete.android.study;

import java.util.List;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class Utilities {

	/*
	 * Get all installed application on mobile and return a list
	 * @param	c	Context of application
	 * @return	list of installed applications
	 */
	public static List getInstalledApplication(Context c) {
		return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
	}

	/*
	 * Launch an application
	 * @param	c	Context of application
	 * @param	pm	the related package manager of the context
	 * @param	pkgName	Name of the package to run
	 */
	public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
		// query the intent for lauching
		Intent intent = pm.getLaunchIntentForPackage(pkgName);
		// if intent is available
		if(intent != null) {
			try {
				// launch application
				c.startActivity(intent);
				// if succeed
				return true;

			// if fail
			} catch(ActivityNotFoundException ex) {
				// quick message notification
				Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
				// display message
				toast.show();
			}
		}
		// by default, fail to launch
		return false;
	}
}

3. MainActivity: (MainActivity.java)

package pete.android.study;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private ListView mListAppInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set layout for the main screen
        setContentView(R.layout.layout_main);

        // load list application
        mListAppInfo = (ListView)findViewById(R.id.lvApps);
        // create new adapter
        AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
        // set adapter to list view
        mListAppInfo.setAdapter(adapter);
        // implement event when an item on list view is selected
        mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView parent, View view, int pos, long id) {
        		// get the list adapter
        		AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
        		// get selected item on the list
        		ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
        		// launch the selected application
        		Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        	}
		});
    }
}

F – Note

– Using the tip of retrieving the list of installed application.

– Using the tip of launching an activity from another.

– Performance is not so good :), of course. You can think of better solution than mine!

– The code is toooo simple, I guess!

G – Get the Sample Project by Pete

Pick your favorite file hosting server: Mediafire | Rapidshare | Megaupload

H – Final Words

– Have you learned something?

– Is it easy to understand?

– Make sure you practice day by day with me :D, it’s fun, isn’t it?

– Hope you enjoy it!

Cheers,

Pete Houston