FileDocCategorySizeDatePackage
StkMenuActivity.javaAPI DocAndroid 1.5 API10329Wed May 06 22:42:48 BST 2009com.android.stk

StkMenuActivity

public class StkMenuActivity extends android.app.ListActivity
ListActivity used for displaying STK menus. These can be SET UP MENU and SELECT ITEM menus. This activity is started multiple times with different menu content.

Fields Summary
private android.content.Context
mContext
private com.android.internal.telephony.gsm.stk.Menu
mStkMenu
private int
mState
private boolean
mAcceptUsersInput
private android.widget.TextView
mTitleTextView
private android.widget.ImageView
mTitleIconView
private android.widget.ProgressBar
mProgressView
StkAppService
appService
static final int
STATE_MAIN
static final int
STATE_SECONDARY
private static final int
MSG_ID_TIMEOUT
android.os.Handler
mTimeoutHandler
Constructors Summary
Methods Summary
private voidcancelTimeOut()

        mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
    
private voiddisplayMenu()


        if (mStkMenu != null) {
            // Display title & title icon
            if (mStkMenu.titleIcon != null) {
                mTitleIconView.setImageBitmap(mStkMenu.titleIcon);
                mTitleIconView.setVisibility(View.VISIBLE);
            } else {
                mTitleIconView.setVisibility(View.GONE);
            }
            if (!mStkMenu.titleIconSelfExplanatory) {
                mTitleTextView.setVisibility(View.VISIBLE);
                if (mStkMenu.title == null) {
                    mTitleTextView.setText(R.string.app_name);
                } else {
                    mTitleTextView.setText(mStkMenu.title);
                }
            } else {
                mTitleTextView.setVisibility(View.INVISIBLE);
            }
            // create an array adapter for the menu list
            StkMenuAdapter adapter = new StkMenuAdapter(this,
                    mStkMenu.items, mStkMenu.itemsIconSelfExplanatory);
            // Bind menu list to the new adapter.
            setListAdapter(adapter);
            // Set default item
            setSelection(mStkMenu.defaultItem);
        }
    
private com.android.internal.telephony.gsm.stk.ItemgetSelectedItem(int position)

        Item item = null;
        if (mStkMenu != null) {
            try {
                item = mStkMenu.items.get(position);
            } catch (IndexOutOfBoundsException e) {
                if (StkApp.DBG) {
                    StkLog.d(this, "Invalid menu");
                }
            } catch (NullPointerException e) {
                if (StkApp.DBG) {
                    StkLog.d(this, "Invalid menu");
                }
            }
        }
        return item;
    
private voidinitFromIntent(android.content.Intent intent)


        if (intent != null) {
            mState = intent.getIntExtra("STATE", STATE_MAIN);
        } else {
            finish();
        }
    
public voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);

        StkLog.d(this, "onCreate");
        // Remove the default title, customized one is used.
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        // Set the layout for this activity.
        setContentView(R.layout.stk_menu_list);

        mTitleTextView = (TextView) findViewById(R.id.title_text);
        mTitleIconView = (ImageView) findViewById(R.id.title_icon);
        mProgressView = (ProgressBar) findViewById(R.id.progress_bar);
        mContext = getBaseContext();

        initFromIntent(getIntent());
        mAcceptUsersInput = true;
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);
        menu.add(0, StkApp.MENU_ID_END_SESSION, 1, R.string.menu_end_session);
        menu.add(0, StkApp.MENU_ID_HELP, 2, R.string.help);
        return true;
    
public voidonDestroy()

        super.onDestroy();

        StkLog.d(this, "onDestroy");
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)


        if (!mAcceptUsersInput) {
            return true;
        }

        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            switch (mState) {
            case STATE_SECONDARY:
                cancelTimeOut();
                mAcceptUsersInput = false;
                sendResponse(StkAppService.RES_ID_BACKWARD);
                return true;
            case STATE_MAIN:
                break;
            }
            break;
        }
        return super.onKeyDown(keyCode, event);
    
protected voidonListItemClick(android.widget.ListView l, android.view.View v, int position, long id)

        super.onListItemClick(l, v, position, id);
 
        if (!mAcceptUsersInput) {
            return;
        }

        Item item = getSelectedItem(position);
        if (item == null) {
            return;
        }
        sendResponse(StkAppService.RES_ID_MENU_SELECTION, item.id, false);
        mAcceptUsersInput = false;
        mProgressView.setVisibility(View.VISIBLE);
        mProgressView.setIndeterminate(true);
    
protected voidonNewIntent(android.content.Intent intent)

        super.onNewIntent(intent);

        StkLog.d(this, "onNewIntent");
        initFromIntent(intent);
        mAcceptUsersInput = true;
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        if (!mAcceptUsersInput) {
            return true;
        }
        switch (item.getItemId()) {
        case StkApp.MENU_ID_END_SESSION:
            cancelTimeOut();
            mAcceptUsersInput = false;
            // send session end response.
            sendResponse(StkAppService.RES_ID_END_SESSION);
            return true;
        case StkApp.MENU_ID_HELP:
            cancelTimeOut();
            mAcceptUsersInput = false;
            int position = getSelectedItemPosition();
            Item stkItem = getSelectedItem(position);
            if (stkItem == null) {
                break;
            }
            // send help needed response.
            sendResponse(StkAppService.RES_ID_MENU_SELECTION, stkItem.id, true);
            return true;
        }
        return super.onOptionsItemSelected(item);
    
public voidonPause()

        super.onPause();

        appService.indicateMenuVisibility(false);
        cancelTimeOut();
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        super.onPrepareOptionsMenu(menu);
        boolean helpVisible = false;
        boolean mainVisible = false;

        if (mState == STATE_SECONDARY) {
            mainVisible = true;
        }
        if (mStkMenu != null) {
            helpVisible = mStkMenu.helpAvailable;
        }

        menu.findItem(StkApp.MENU_ID_END_SESSION).setVisible(mainVisible);
        menu.findItem(StkApp.MENU_ID_HELP).setVisible(helpVisible);

        return true;
    
protected voidonRestoreInstanceState(android.os.Bundle savedInstanceState)

        mState = savedInstanceState.getInt("STATE");
        mStkMenu = savedInstanceState.getParcelable("MENU");
    
public voidonResume()

        super.onResume();

        appService.indicateMenuVisibility(true);
        mStkMenu = appService.getMenu();
        if (mStkMenu == null) {
            finish();
            return;
        }
        displayMenu();
        startTimeOut();
        // whenever this activity is resumed after a sub activity was invoked
        // (Browser, In call screen) switch back to main state and enable  
        // user's input;
        if (!mAcceptUsersInput) {
            mState = STATE_MAIN;
            mAcceptUsersInput = true;
        }
        // make sure the progress bar is not shown.
        mProgressView.setIndeterminate(false);
        mProgressView.setVisibility(View.GONE);
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        outState.putInt("STATE", mState);
        outState.putParcelable("MENU", mStkMenu);
    
private voidsendResponse(int resId)

        sendResponse(resId, 0, false);
    
private voidsendResponse(int resId, int itemId, boolean help)

        Bundle args = new Bundle();
        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
        args.putInt(StkAppService.RES_ID, resId);
        args.putInt(StkAppService.MENU_SELECTION, itemId);
        args.putBoolean(StkAppService.HELP, help);
        mContext.startService(new Intent(mContext, StkAppService.class)
                .putExtras(args));
    
private voidstartTimeOut()

        if (mState == STATE_SECONDARY) {
            // Reset timeout.
            cancelTimeOut();
            mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
                    .obtainMessage(MSG_ID_TIMEOUT), StkApp.UI_TIMEOUT);
        }