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