19d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project/*
29d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project *
49d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * you may not use this file except in compliance with the License.
69d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * You may obtain a copy of the License at
79d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project *
89d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project *
109d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * See the License for the specific language governing permissions and
149d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * limitations under the License.
159d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project */
169d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
179d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectpackage com.android.stk;
189d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
199d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.app.ListActivity;
209d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.content.Context;
219d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.content.Intent;
229d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.os.Bundle;
239d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.os.Handler;
249d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.os.Message;
259d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.view.KeyEvent;
269d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.view.MenuItem;
279d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.view.View;
289d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.view.Window;
299d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.widget.ImageView;
309d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.widget.ListView;
319d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.widget.ProgressBar;
329d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.widget.TextView;
339d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
349d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport com.android.internal.telephony.gsm.stk.Item;
359d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport com.android.internal.telephony.gsm.stk.Menu;
369d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport com.android.internal.telephony.gsm.stk.StkLog;
379d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
389d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project/**
3979085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville * ListActivity used for displaying STK menus. These can be SET UP MENU and
4079085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville * SELECT ITEM menus. This activity is started multiple times with different
419d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * menu content.
429d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project *
439d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project */
449d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectpublic class StkMenuActivity extends ListActivity {
459d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private Context mContext;
469d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private Menu mStkMenu = null;
479d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private int mState = STATE_MAIN;
489d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private boolean mAcceptUsersInput = true;
499d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
509d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private TextView mTitleTextView = null;
519d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private ImageView mTitleIconView = null;
529d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private ProgressBar mProgressView = null;
539d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
549d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    StkAppService appService = StkAppService.getInstance();
559d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
569d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    // Internal state values
579d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    static final int STATE_MAIN = 1;
589d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    static final int STATE_SECONDARY = 2;
599d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
6079085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville    // message id for time out
619d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private static final int MSG_ID_TIMEOUT = 1;
629d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
639d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    Handler mTimeoutHandler = new Handler() {
649d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        @Override
659d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        public void handleMessage(Message msg) {
669d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            switch(msg.what) {
679d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            case MSG_ID_TIMEOUT:
689d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mAcceptUsersInput = false;
699d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                sendResponse(StkAppService.RES_ID_TIMEOUT);
709d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                break;
719d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
729d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
739d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    };
749d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
759d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
769d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public void onCreate(Bundle icicle) {
779d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onCreate(icicle);
789d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
799d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        StkLog.d(this, "onCreate");
809d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        // Remove the default title, customized one is used.
8179085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville        requestWindowFeature(Window.FEATURE_NO_TITLE);
829d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        // Set the layout for this activity.
839d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        setContentView(R.layout.stk_menu_list);
849d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
859d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mTitleTextView = (TextView) findViewById(R.id.title_text);
869d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mTitleIconView = (ImageView) findViewById(R.id.title_icon);
879d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mProgressView = (ProgressBar) findViewById(R.id.progress_bar);
889d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mContext = getBaseContext();
899d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
909d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        initFromIntent(getIntent());
919d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mAcceptUsersInput = true;
929d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
939d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
949d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
959d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    protected void onNewIntent(Intent intent) {
969d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onNewIntent(intent);
979d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
989d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        StkLog.d(this, "onNewIntent");
999d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        initFromIntent(intent);
1009d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mAcceptUsersInput = true;
1019d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1029d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1039d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1049d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    protected void onListItemClick(ListView l, View v, int position, long id) {
1059d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onListItemClick(l, v, position, id);
10679085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville
1079d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (!mAcceptUsersInput) {
1089d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return;
1099d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1109d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1119d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        Item item = getSelectedItem(position);
1129d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (item == null) {
1139d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return;
1149d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1159d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        sendResponse(StkAppService.RES_ID_MENU_SELECTION, item.id, false);
1169d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mAcceptUsersInput = false;
1179d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mProgressView.setVisibility(View.VISIBLE);
1189d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mProgressView.setIndeterminate(true);
1199d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1209d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1219d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1229d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public boolean onKeyDown(int keyCode, KeyEvent event) {
1239d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1249d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (!mAcceptUsersInput) {
1259d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return true;
1269d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1279d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1289d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        switch (keyCode) {
1299d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        case KeyEvent.KEYCODE_BACK:
1309d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            switch (mState) {
1319d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            case STATE_SECONDARY:
1329d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                cancelTimeOut();
1339d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mAcceptUsersInput = false;
1349d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                sendResponse(StkAppService.RES_ID_BACKWARD);
1359d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                return true;
1369d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            case STATE_MAIN:
1379d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                break;
1389d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
1399d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            break;
1409d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1419d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        return super.onKeyDown(keyCode, event);
1429d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1439d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1449d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1459d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public void onResume() {
1469d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onResume();
1479d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1489d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        appService.indicateMenuVisibility(true);
1499d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mStkMenu = appService.getMenu();
1509d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mStkMenu == null) {
1519d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            finish();
1529d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return;
1539d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1549d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        displayMenu();
1559d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        startTimeOut();
1569d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        // whenever this activity is resumed after a sub activity was invoked
15779085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville        // (Browser, In call screen) switch back to main state and enable
1589d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        // user's input;
1599d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (!mAcceptUsersInput) {
1609d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mState = STATE_MAIN;
1619d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mAcceptUsersInput = true;
1629d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
1639d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        // make sure the progress bar is not shown.
1649d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mProgressView.setIndeterminate(false);
1659d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mProgressView.setVisibility(View.GONE);
1669d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1679d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1689d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1699d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public void onPause() {
1709d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onPause();
1719d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1729d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        appService.indicateMenuVisibility(false);
1739d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        cancelTimeOut();
1749d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1759d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1769d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1779d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public void onDestroy() {
1789d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onDestroy();
1799d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1809d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        StkLog.d(this, "onDestroy");
1819d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1829d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1839d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1849d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public boolean onCreateOptionsMenu(android.view.Menu menu) {
1859d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onCreateOptionsMenu(menu);
1869d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        menu.add(0, StkApp.MENU_ID_END_SESSION, 1, R.string.menu_end_session);
1879d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        menu.add(0, StkApp.MENU_ID_HELP, 2, R.string.help);
1889d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        return true;
1899d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
1909d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1919d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
1929d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public boolean onPrepareOptionsMenu(android.view.Menu menu) {
1939d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        super.onPrepareOptionsMenu(menu);
1949d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        boolean helpVisible = false;
1959d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        boolean mainVisible = false;
1969d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
1979d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mState == STATE_SECONDARY) {
1989d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mainVisible = true;
1999d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2009d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mStkMenu != null) {
2019d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            helpVisible = mStkMenu.helpAvailable;
2029d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2039d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2049d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        menu.findItem(StkApp.MENU_ID_END_SESSION).setVisible(mainVisible);
2059d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        menu.findItem(StkApp.MENU_ID_HELP).setVisible(helpVisible);
2069d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2079d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        return true;
2089d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2099d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2109d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
2119d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
2129d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (!mAcceptUsersInput) {
2139d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return true;
2149d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2159d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        switch (item.getItemId()) {
2169d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        case StkApp.MENU_ID_END_SESSION:
2179d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            cancelTimeOut();
2189d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mAcceptUsersInput = false;
2199d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // send session end response.
2209d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            sendResponse(StkAppService.RES_ID_END_SESSION);
2219d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return true;
2229d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        case StkApp.MENU_ID_HELP:
2239d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            cancelTimeOut();
2249d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mAcceptUsersInput = false;
2259d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            int position = getSelectedItemPosition();
2269d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            Item stkItem = getSelectedItem(position);
2279d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            if (stkItem == null) {
2289d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                break;
2299d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
2309d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // send help needed response.
2319d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            sendResponse(StkAppService.RES_ID_MENU_SELECTION, stkItem.id, true);
2329d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            return true;
2339d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2349d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        return super.onOptionsItemSelected(item);
2359d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2369d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2379d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
2389d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    protected void onSaveInstanceState(Bundle outState) {
2399d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        outState.putInt("STATE", mState);
2409d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        outState.putParcelable("MENU", mStkMenu);
2419d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2429d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2439d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
2449d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    protected void onRestoreInstanceState(Bundle savedInstanceState) {
2459d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mState = savedInstanceState.getInt("STATE");
2469d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mStkMenu = savedInstanceState.getParcelable("MENU");
2479d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
24879085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville
2499d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void cancelTimeOut() {
2509d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mTimeoutHandler.removeMessages(MSG_ID_TIMEOUT);
2519d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2529d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2539d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void startTimeOut() {
2549d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mState == STATE_SECONDARY) {
2559d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // Reset timeout.
2569d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            cancelTimeOut();
2579d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mTimeoutHandler.sendMessageDelayed(mTimeoutHandler
2589d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    .obtainMessage(MSG_ID_TIMEOUT), StkApp.UI_TIMEOUT);
2599d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2609d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
26179085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville
2629d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    // Bind list adapter to the items list.
2639d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void displayMenu() {
2649d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2659d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mStkMenu != null) {
2669d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // Display title & title icon
2679d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            if (mStkMenu.titleIcon != null) {
2689d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mTitleIconView.setImageBitmap(mStkMenu.titleIcon);
2699d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mTitleIconView.setVisibility(View.VISIBLE);
2709d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            } else {
2719d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mTitleIconView.setVisibility(View.GONE);
2729d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
2739d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            if (!mStkMenu.titleIconSelfExplanatory) {
2749d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mTitleTextView.setVisibility(View.VISIBLE);
2759d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                if (mStkMenu.title == null) {
2769d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    mTitleTextView.setText(R.string.app_name);
2779d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                } else {
2789d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    mTitleTextView.setText(mStkMenu.title);
2799d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                }
2809d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            } else {
2819d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                mTitleTextView.setVisibility(View.INVISIBLE);
2829d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
2839d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // create an array adapter for the menu list
2849d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            StkMenuAdapter adapter = new StkMenuAdapter(this,
2859d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    mStkMenu.items, mStkMenu.itemsIconSelfExplanatory);
2869d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // Bind menu list to the new adapter.
2879d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            setListAdapter(adapter);
2889d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            // Set default item
2899d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            setSelection(mStkMenu.defaultItem);
2909d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
2919d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2929d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2939d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void initFromIntent(Intent intent) {
2949d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
2959d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (intent != null) {
2969d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            mState = intent.getIntExtra("STATE", STATE_MAIN);
2979d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        } else {
2989d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            finish();
2999d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
3009d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
3019d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
3029d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private Item getSelectedItem(int position) {
3039d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        Item item = null;
3049d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        if (mStkMenu != null) {
3059d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            try {
3069d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                item = mStkMenu.items.get(position);
3079d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            } catch (IndexOutOfBoundsException e) {
3089d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                if (StkApp.DBG) {
3099d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    StkLog.d(this, "Invalid menu");
3109d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                }
3119d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            } catch (NullPointerException e) {
3129d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                if (StkApp.DBG) {
3139d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                    StkLog.d(this, "Invalid menu");
3149d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                }
3159d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project            }
3169d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        }
3179d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        return item;
3189d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
3199d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
3209d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void sendResponse(int resId) {
3219d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        sendResponse(resId, 0, false);
3229d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
3239d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
3249d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    private void sendResponse(int resId, int itemId, boolean help) {
3259d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        Bundle args = new Bundle();
3269d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        args.putInt(StkAppService.OPCODE, StkAppService.OP_RESPONSE);
3279d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        args.putInt(StkAppService.RES_ID, resId);
3289d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        args.putInt(StkAppService.MENU_SELECTION, itemId);
3299d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        args.putBoolean(StkAppService.HELP, help);
3309d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        mContext.startService(new Intent(mContext, StkAppService.class)
3319d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project                .putExtras(args));
3329d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
3339d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project}
334