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