StkLauncherActivity.java revision e68857da85f6fb0964da9d58a7cbfcc0348065c6
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.stk;
18
19import android.app.ListActivity;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Bundle;
23import android.view.View;
24import android.view.KeyEvent;
25import android.view.Window;
26import android.widget.ImageView;
27import android.widget.ListView;
28import android.widget.TextView;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31
32import com.android.internal.telephony.cat.Item;
33import com.android.internal.telephony.cat.Menu;
34import com.android.internal.telephony.cat.CatLog;
35import com.android.internal.telephony.PhoneConstants;
36
37import android.telephony.TelephonyManager;
38
39import java.util.ArrayList;
40
41/**
42 * Launcher class. Serve as the app's MAIN activity, send an intent to the
43 * StkAppService and finish.
44 *
45 */
46public class StkLauncherActivity extends ListActivity {
47    private TextView mTitleTextView = null;
48    private ImageView mTitleIconView = null;
49    private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
50    private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
51    private ArrayList<Item> mStkMenuList = null;
52    private int mSingleSimId = -1;
53    private Context mContext = null;
54    private TelephonyManager mTm = null;
55    private Bitmap mBitMap = null;
56    private boolean mAcceptUsersInput = true;
57
58    @Override
59    public void onCreate(Bundle icicle) {
60        super.onCreate(icicle);
61        CatLog.d(LOG_TAG, "onCreate+");
62        mContext = getBaseContext();
63        mTm = (TelephonyManager) mContext.getSystemService(
64                Context.TELEPHONY_SERVICE);
65        //Check if needs to show the menu list.
66        if (isShowSTKListMenu()) {
67            requestWindowFeature(Window.FEATURE_NO_TITLE);
68            setContentView(R.layout.stk_menu_list);
69            mTitleTextView = (TextView) findViewById(R.id.title_text);
70            mTitleIconView = (ImageView) findViewById(R.id.title_icon);
71            mTitleTextView.setText(R.string.app_name);
72            mBitMap = BitmapFactory.decodeResource(getResources(),
73                    R.drawable.ic_launcher_sim_toolkit);
74        } else {
75            //launch stk menu activity for the SIM.
76            if (mSingleSimId < 0) {
77                finish();
78            }
79        }
80    }
81
82    @Override
83    protected void onNewIntent(Intent intent) {
84        super.onNewIntent(intent);
85    }
86
87    @Override
88    protected void onListItemClick(ListView l, View v, int position, long id) {
89        super.onListItemClick(l, v, position, id);
90        if (!mAcceptUsersInput) {
91            CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
92            return;
93        }
94        int simCount = TelephonyManager.from(mContext).getSimCount();
95        Item item = getSelectedItem(position);
96        if (item == null) {
97            CatLog.d(LOG_TAG, "Item is null");
98            return;
99        }
100        CatLog.d(LOG_TAG, "launch stk menu id: " + item.id);
101        if (item.id >= PhoneConstants.SIM_ID_1 && item.id < simCount) {
102            mAcceptUsersInput = false;
103            launchSTKMainMenu(item.id);
104        }
105    }
106
107    @Override
108    public boolean onKeyDown(int keyCode, KeyEvent event) {
109        CatLog.d(LOG_TAG, "mAcceptUsersInput: " + mAcceptUsersInput);
110        if (!mAcceptUsersInput) {
111            return true;
112        }
113        switch (keyCode) {
114            case KeyEvent.KEYCODE_BACK:
115                CatLog.d(LOG_TAG, "KEYCODE_BACK.");
116                mAcceptUsersInput = false;
117                finish();
118                return true;
119        }
120        return super.onKeyDown(keyCode, event);
121    }
122
123    @Override
124    public void onResume() {
125        super.onResume();
126        CatLog.d(LOG_TAG, "onResume");
127        mAcceptUsersInput = true;
128        int itemSize = addStkMenuListItems();
129        if (itemSize == 0) {
130            CatLog.d(LOG_TAG, "item size = 0 so finish.");
131            finish();
132        } else if (itemSize == 1) {
133            launchSTKMainMenu(mSingleSimId);
134            finish();
135        } else {
136            CatLog.d(LOG_TAG, "resume to show multiple stk list.");
137        }
138    }
139
140    @Override
141    public void onPause() {
142        super.onPause();
143        CatLog.d(LOG_TAG, "onPause");
144    }
145
146    @Override
147    public void onDestroy() {
148        super.onDestroy();
149        CatLog.d(LOG_TAG, "onDestroy");
150    }
151
152    private Item getSelectedItem(int position) {
153        Item item = null;
154        if (mStkMenuList != null) {
155            try {
156                item = mStkMenuList.get(position);
157            } catch (IndexOutOfBoundsException e) {
158                if (StkApp.DBG) {
159                    CatLog.d(LOG_TAG, "IOOBE Invalid menu");
160                }
161            } catch (NullPointerException e) {
162                if (StkApp.DBG) {
163                    CatLog.d(LOG_TAG, "NPE Invalid menu");
164                }
165            }
166        }
167        return item;
168    }
169
170    private int addStkMenuListItems() {
171        String appName = mContext.getResources().getString(R.string.app_name);
172        String stkItemName = null;
173        int simCount = TelephonyManager.from(mContext).getSimCount();
174        mStkMenuList = new ArrayList<Item>();
175
176        CatLog.d(LOG_TAG, "simCount: " + simCount);
177        for (int i = 0; i < simCount; i++) {
178            //Check if the card is inserted.
179            if (mTm.hasIccCard(i)) {
180                CatLog.d(LOG_TAG, "SIM " + i + " add to menu.");
181                mSingleSimId = i;
182                stkItemName = new StringBuilder(appName).append(" ")
183                        .append(Integer.toString(i + 1)).toString();
184                Item item = new Item(i + 1, stkItemName, mBitMap);
185                item.id = i;
186                mStkMenuList.add(item);
187            } else {
188                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
189            }
190        }
191        if (mStkMenuList != null && mStkMenuList.size() > 0) {
192            if (mStkMenuList.size() > 1) {
193                StkMenuAdapter adapter = new StkMenuAdapter(this,
194                        mStkMenuList, false);
195                // Bind menu list to the new adapter.
196                this.setListAdapter(adapter);
197            }
198            return mStkMenuList.size();
199        } else {
200            CatLog.d(LOG_TAG, "No stk menu item add.");
201            return 0;
202        }
203    }
204    private boolean isShowSTKListMenu() {
205        int simCount = TelephonyManager.from(mContext).getSimCount();
206        int simInsertedCount = 0;
207        int insertedSlotId = -1;
208
209        CatLog.d(LOG_TAG, "simCount: " + simCount);
210        for (int i = 0; i < simCount; i++) {
211            //Check if the card is inserted.
212            if (mTm.hasIccCard(i)) {
213                CatLog.d(LOG_TAG, "SIM " + i + " is inserted.");
214                mSingleSimId = i;
215                simInsertedCount++;
216            } else {
217                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
218            }
219        }
220        if (simInsertedCount > 1) {
221            return true;
222        } else {
223            //No card or only one card.
224            CatLog.d(LOG_TAG, "do not show stk list menu.");
225            return false;
226        }
227    }
228    private void launchSTKMainMenu(int slodId) {
229        Bundle args = new Bundle();
230        CatLog.d(LOG_TAG, "launchSTKMainMenu.");
231        args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
232        args.putInt(StkAppService.SLOT_ID
233                , PhoneConstants.SIM_ID_1 + slodId);
234        startService(new Intent(this, StkAppService.class)
235                .putExtras(args));
236    }
237}
238