FileDocCategorySizeDatePackage
RecentApplicationsDialog.javaAPI DocAndroid 1.5 API9599Wed May 06 22:42:06 BST 2009com.android.internal.policy.impl

RecentApplicationsDialog

public class RecentApplicationsDialog extends android.app.Dialog implements android.view.View.OnClickListener

Fields Summary
private static final boolean
DBG_FORCE_EMPTY_LIST
private static android.app.StatusBarManager
sStatusBar
private static final int
NUM_BUTTONS
private static final int
MAX_RECENT_TASKS
final android.view.View[]
mButtons
android.view.View
mNoAppsText
android.content.IntentFilter
mBroadcastIntentFilter
private android.content.BroadcastReceiver
mBroadcastReceiver
This is the listener for the ACTION_CLOSE_SYSTEM_DIALOGS intent. It's an indication that we should close ourselves immediately, in order to allow a higher-priority UI to take over (e.g. phone call received). TODO: This is a really heavyweight solution for something that should be so simple. For example, we already have a handler, in our superclass, why aren't we sharing that? I think we need to investigate simplifying this entire methodology, or perhaps boosting it up into the Dialog class.
Constructors Summary
public RecentApplicationsDialog(android.content.Context context)


       
        super(context);
    
Methods Summary
public voidonClick(android.view.View v)
Handler for user clicks. If a button was clicked, launch the corresponding activity.

        
        for (View b : mButtons) {
            if (b == v) {
                // prepare a launch intent and send it
                Intent intent = (Intent)b.getTag();
                getContext().startActivity(intent);
            }
        }
        dismiss();
    
protected voidonCreate(android.os.Bundle savedInstanceState)
We create the recent applications dialog just once, and it stays around (hidden) until activated by the user.

see
MidWindowManager#showRecentAppsDialog

        super.onCreate(savedInstanceState);

        Context context = getContext();

        if (sStatusBar == null) {
            sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
        }

        Window theWindow = getWindow();
        theWindow.requestFeature(Window.FEATURE_NO_TITLE);
        theWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        theWindow.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                           WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        
        setContentView(com.android.internal.R.layout.recent_apps_dialog);

        mButtons[0] = findViewById(com.android.internal.R.id.button1);
        mButtons[1] = findViewById(com.android.internal.R.id.button2);
        mButtons[2] = findViewById(com.android.internal.R.id.button3);
        mButtons[3] = findViewById(com.android.internal.R.id.button4);
        mButtons[4] = findViewById(com.android.internal.R.id.button5);
        mButtons[5] = findViewById(com.android.internal.R.id.button6);
        mNoAppsText = findViewById(com.android.internal.R.id.no_applications_message);
        
        for (View b : mButtons) {
            b.setOnClickListener(this);
        }
    
public voidonStart()
Set up and show the recent activities dialog.

        super.onStart();
        reloadButtons();
        if (sStatusBar != null) {
            sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
        }

        // receive broadcasts
        getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter);
    
public voidonStop()
Dismiss the recent activities dialog.

        super.onStop();
        
        // dump extra memory we're hanging on to
        for (View b : mButtons) {
            setButtonAppearance(b, null, null);
            b.setTag(null);
        }

        if (sStatusBar != null) {
            sStatusBar.disable(StatusBarManager.DISABLE_NONE);
        }

        // stop receiving broadcasts
        getContext().unregisterReceiver(mBroadcastReceiver);
     
private voidreloadButtons()
Reload the 6 buttons with recent activities

        
        final Context context = getContext();
        final PackageManager pm = context.getPackageManager();
        final ActivityManager am = (ActivityManager) 
                                        context.getSystemService(Context.ACTIVITY_SERVICE);
        final List<ActivityManager.RecentTaskInfo> recentTasks = 
                                        am.getRecentTasks(MAX_RECENT_TASKS, 0);
        
        ResolveInfo homeInfo = pm.resolveActivity(
                new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),
                0);
        
        // Performance note:  Our android performance guide says to prefer Iterator when
        // using a List class, but because we know that getRecentTasks() always returns
        // an ArrayList<>, we'll use a simple index instead.
        int button = 0;
        int numTasks = recentTasks.size();
        for (int i = 0; i < numTasks && (button < NUM_BUTTONS); ++i) {
            final ActivityManager.RecentTaskInfo info = recentTasks.get(i);
            
            // for debug purposes only, disallow first result to create empty lists
            if (DBG_FORCE_EMPTY_LIST && (i == 0)) continue;
            
            Intent intent = new Intent(info.baseIntent);
            if (info.origActivity != null) {
                intent.setComponent(info.origActivity);
            }
            
            // Skip the current home activity.
            if (homeInfo != null) {
                if (homeInfo.activityInfo.packageName.equals(
                        intent.getComponent().getPackageName())
                        && homeInfo.activityInfo.name.equals(
                                intent.getComponent().getClassName())) {
                    continue;
                }
            }
            
            intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
            final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
            if (resolveInfo != null) {
                final ActivityInfo activityInfo = resolveInfo.activityInfo;
                final String title = activityInfo.loadLabel(pm).toString();
                final Drawable icon = activityInfo.loadIcon(pm);

                if (title != null && title.length() > 0 && icon != null) {
                    final View b = mButtons[button];
                    setButtonAppearance(b, title, icon);
                    b.setTag(intent);
                    b.setVisibility(View.VISIBLE);
                    b.setPressed(false);
                    b.clearFocus();
                    ++button;
                }
            }
        }
        
        // handle the case of "no icons to show"
        mNoAppsText.setVisibility((button == 0) ? View.VISIBLE : View.GONE);
        
        // hide the rest
        for ( ; button < NUM_BUTTONS; ++button) {
            mButtons[button].setVisibility(View.GONE);
        }
    
private voidsetButtonAppearance(android.view.View theButton, java.lang.String theTitle, android.graphics.drawable.Drawable icon)
Adjust appearance of each icon-button

        TextView tv = (TextView) theButton.findViewById(com.android.internal.R.id.label);
        tv.setText(theTitle);
        ImageView iv = (ImageView) theButton.findViewById(com.android.internal.R.id.icon);
        iv.setImageDrawable(icon);