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
19e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.app.ListActivity;
20e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.content.Context;
219d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.content.Intent;
229d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Projectimport android.os.Bundle;
23e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.view.View;
24e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.view.KeyEvent;
25e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.view.Window;
26e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.widget.ImageView;
27e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.widget.ListView;
28e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.widget.TextView;
29e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.graphics.Bitmap;
30e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.graphics.BitmapFactory;
31e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
32e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport com.android.internal.telephony.cat.Item;
33e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport com.android.internal.telephony.cat.Menu;
34e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport com.android.internal.telephony.cat.CatLog;
35e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport com.android.internal.telephony.PhoneConstants;
36e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
37e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport android.telephony.TelephonyManager;
38e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
39e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savilleimport java.util.ArrayList;
409d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
419d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project/**
4279085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville * Launcher class. Serve as the app's MAIN activity, send an intent to the
439d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project * StkAppService and finish.
4479085fcc5b4e54cc374e8720644ce4c1dc0004beWink Saville *
459d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project */
46e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Savillepublic class StkLauncherActivity extends ListActivity {
47e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private TextView mTitleTextView = null;
48e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private ImageView mTitleIconView = null;
49e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
50e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
51e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private ArrayList<Item> mStkMenuList = null;
52e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private int mSingleSimId = -1;
53e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private Context mContext = null;
54e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private TelephonyManager mTm = null;
55e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private Bitmap mBitMap = null;
56e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private boolean mAcceptUsersInput = true;
57e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
58e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
59e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    public void onCreate(Bundle icicle) {
60e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onCreate(icicle);
61e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "onCreate+");
62e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        mContext = getBaseContext();
63e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        mTm = (TelephonyManager) mContext.getSystemService(
64e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                Context.TELEPHONY_SERVICE);
65aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        requestWindowFeature(Window.FEATURE_NO_TITLE);
66aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        setContentView(R.layout.stk_menu_list);
67aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        mTitleTextView = (TextView) findViewById(R.id.title_text);
68aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        mTitleIconView = (ImageView) findViewById(R.id.title_icon);
69aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        mTitleTextView.setText(R.string.app_name);
70aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu        mBitMap = BitmapFactory.decodeResource(getResources(),
71aeefef5980a88d0dff10f4c81300789dca4aa1a5Legler Wu                R.drawable.ic_launcher_sim_toolkit);
72e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
73e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
74e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
75e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    protected void onNewIntent(Intent intent) {
76e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onNewIntent(intent);
77e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
78e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
79e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
80e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    protected void onListItemClick(ListView l, View v, int position, long id) {
81e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onListItemClick(l, v, position, id);
82e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (!mAcceptUsersInput) {
83e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
84e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            return;
85e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
86e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        int simCount = TelephonyManager.from(mContext).getSimCount();
87e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        Item item = getSelectedItem(position);
88e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (item == null) {
89e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            CatLog.d(LOG_TAG, "Item is null");
90e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            return;
91e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
92e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "launch stk menu id: " + item.id);
93e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (item.id >= PhoneConstants.SIM_ID_1 && item.id < simCount) {
94e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            mAcceptUsersInput = false;
95e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            launchSTKMainMenu(item.id);
96e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
97e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
98e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
99e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
100e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    public boolean onKeyDown(int keyCode, KeyEvent event) {
101e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "mAcceptUsersInput: " + mAcceptUsersInput);
102e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (!mAcceptUsersInput) {
103e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            return true;
104e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
105e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        switch (keyCode) {
106e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            case KeyEvent.KEYCODE_BACK:
107e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                CatLog.d(LOG_TAG, "KEYCODE_BACK.");
108e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                mAcceptUsersInput = false;
109e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                finish();
110e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                return true;
111e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
112e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        return super.onKeyDown(keyCode, event);
113e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
114e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
115e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
116e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    public void onResume() {
117e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onResume();
118e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "onResume");
119e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        mAcceptUsersInput = true;
120e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        int itemSize = addStkMenuListItems();
121e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (itemSize == 0) {
122e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            CatLog.d(LOG_TAG, "item size = 0 so finish.");
123e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            finish();
124e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        } else if (itemSize == 1) {
125e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            launchSTKMainMenu(mSingleSimId);
126e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            finish();
127e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        } else {
128e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            CatLog.d(LOG_TAG, "resume to show multiple stk list.");
129e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
130e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
131e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
1329d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    @Override
133e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    public void onPause() {
134e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onPause();
135e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "onPause");
136e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
137e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
138e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    @Override
139e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    public void onDestroy() {
140e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        super.onDestroy();
141e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "onDestroy");
142e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
1439d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project
144e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private Item getSelectedItem(int position) {
145e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        Item item = null;
146e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (mStkMenuList != null) {
147e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            try {
148e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                item = mStkMenuList.get(position);
149e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            } catch (IndexOutOfBoundsException e) {
150e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                if (StkApp.DBG) {
151e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                    CatLog.d(LOG_TAG, "IOOBE Invalid menu");
152e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                }
153e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            } catch (NullPointerException e) {
154e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                if (StkApp.DBG) {
155e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                    CatLog.d(LOG_TAG, "NPE Invalid menu");
156e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                }
157e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            }
158e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
159e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        return item;
160e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
161e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
162e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private int addStkMenuListItems() {
163e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        String appName = mContext.getResources().getString(R.string.app_name);
164e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        String stkItemName = null;
165e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        int simCount = TelephonyManager.from(mContext).getSimCount();
166e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        mStkMenuList = new ArrayList<Item>();
167e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville
168e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "simCount: " + simCount);
169e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        for (int i = 0; i < simCount; i++) {
170e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            //Check if the card is inserted.
171e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            if (mTm.hasIccCard(i)) {
172e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                CatLog.d(LOG_TAG, "SIM " + i + " add to menu.");
173e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                mSingleSimId = i;
174e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                stkItemName = new StringBuilder(appName).append(" ")
175e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                        .append(Integer.toString(i + 1)).toString();
176e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                Item item = new Item(i + 1, stkItemName, mBitMap);
177e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                item.id = i;
178e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                mStkMenuList.add(item);
179e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            } else {
180e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
181e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            }
182e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
183e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        if (mStkMenuList != null && mStkMenuList.size() > 0) {
184e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            if (mStkMenuList.size() > 1) {
185e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                StkMenuAdapter adapter = new StkMenuAdapter(this,
186e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                        mStkMenuList, false);
187e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                // Bind menu list to the new adapter.
188e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                this.setListAdapter(adapter);
189e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            }
190e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            return mStkMenuList.size();
191e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        } else {
192e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            CatLog.d(LOG_TAG, "No stk menu item add.");
193e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville            return 0;
194e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        }
195e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    }
196e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville    private void launchSTKMainMenu(int slodId) {
1979d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        Bundle args = new Bundle();
198e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        CatLog.d(LOG_TAG, "launchSTKMainMenu.");
1999d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project        args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
200e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        args.putInt(StkAppService.SLOT_ID
201e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                , PhoneConstants.SIM_ID_1 + slodId);
202e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville        startService(new Intent(this, StkAppService.class)
203e68857da85f6fb0964da9d58a7cbfcc0348065c6Wink Saville                .putExtras(args));
2049d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project    }
2059d9730aecb72e830ef12d66ce6e084d8ae0928f2The Android Open Source Project}
206