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.ActionBar;
20 import android.app.ListActivity;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.KeyEvent;
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 LOG_TAG =
50             new Object(){}.getClass().getEnclosingClass().getSimpleName();
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 
66         ActionBar actionBar = getActionBar();
67         actionBar.setCustomView(R.layout.stk_title);
68         actionBar.setDisplayShowCustomEnabled(true);
69 
70         setContentView(R.layout.stk_menu_list);
71         mTitleTextView = (TextView) findViewById(R.id.title_text);
72         mTitleIconView = (ImageView) findViewById(R.id.title_icon);
73         mTitleTextView.setText(R.string.app_name);
74         mBitMap = BitmapFactory.decodeResource(getResources(),
75                 R.drawable.ic_launcher_sim_toolkit);
76     }
77 
78     @Override
onNewIntent(Intent intent)79     protected void onNewIntent(Intent intent) {
80         super.onNewIntent(intent);
81     }
82 
83     @Override
onListItemClick(ListView l, View v, int position, long id)84     protected void onListItemClick(ListView l, View v, int position, long id) {
85         super.onListItemClick(l, v, position, id);
86         if (!mAcceptUsersInput) {
87             CatLog.d(LOG_TAG, "mAcceptUsersInput:false");
88             return;
89         }
90         int simCount = TelephonyManager.from(mContext).getSimCount();
91         Item item = getSelectedItem(position);
92         if (item == null) {
93             CatLog.d(LOG_TAG, "Item is null");
94             return;
95         }
96         CatLog.d(LOG_TAG, "launch stk menu id: " + item.id);
97         if (item.id >= 0 && item.id < simCount) {
98             mAcceptUsersInput = false;
99             launchSTKMainMenu(item.id);
100         }
101     }
102 
103     @Override
onKeyDown(int keyCode, KeyEvent event)104     public boolean onKeyDown(int keyCode, KeyEvent event) {
105         CatLog.d(LOG_TAG, "mAcceptUsersInput: " + mAcceptUsersInput);
106         if (!mAcceptUsersInput) {
107             return true;
108         }
109         switch (keyCode) {
110             case KeyEvent.KEYCODE_BACK:
111                 CatLog.d(LOG_TAG, "KEYCODE_BACK.");
112                 mAcceptUsersInput = false;
113                 finish();
114                 return true;
115         }
116         return super.onKeyDown(keyCode, event);
117     }
118 
119     @Override
onResume()120     public void onResume() {
121         super.onResume();
122         CatLog.d(LOG_TAG, "onResume");
123         mAcceptUsersInput = true;
124         int itemSize = addStkMenuListItems();
125         if (itemSize == 0) {
126             CatLog.d(LOG_TAG, "item size = 0 so finish.");
127             finish();
128         } else if (itemSize == 1) {
129             launchSTKMainMenu(mSingleSimId);
130             finish();
131         } else {
132             CatLog.d(LOG_TAG, "resume to show multiple stk list.");
133         }
134     }
135 
136     @Override
onPause()137     public void onPause() {
138         super.onPause();
139         CatLog.d(LOG_TAG, "onPause");
140     }
141 
142     @Override
onDestroy()143     public void onDestroy() {
144         super.onDestroy();
145         CatLog.d(LOG_TAG, "onDestroy");
146     }
147 
getSelectedItem(int position)148     private Item getSelectedItem(int position) {
149         Item item = null;
150         if (mStkMenuList != null) {
151             try {
152                 item = mStkMenuList.get(position);
153             } catch (IndexOutOfBoundsException e) {
154                 if (StkApp.DBG) {
155                     CatLog.d(LOG_TAG, "IOOBE Invalid menu");
156                 }
157             } catch (NullPointerException e) {
158                 if (StkApp.DBG) {
159                     CatLog.d(LOG_TAG, "NPE Invalid menu");
160                 }
161             }
162         }
163         return item;
164     }
165 
addStkMenuListItems()166     private int addStkMenuListItems() {
167         StkAppService appService = StkAppService.getInstance();
168         if (appService == null) {
169             return 0;
170         }
171 
172         String appName = mContext.getResources().getString(R.string.app_name);
173         String stkItemName = null;
174         int simCount = TelephonyManager.from(mContext).getSimCount();
175         mStkMenuList = new ArrayList<Item>();
176 
177         CatLog.d(LOG_TAG, "simCount: " + simCount);
178         for (int i = 0; i < simCount; i++) {
179             // Check if the card is inserted.
180             if (mTm.hasIccCard(i)) {
181                 Menu menu = appService.getMainMenu(i);
182                 // Check if the card has a main menu.
183                 if (menu != null) {
184                     CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is add to menu.");
185                     mSingleSimId = i;
186                     stkItemName = new StringBuilder(menu.title == null ? appName : menu.title)
187                             .append(" ").append(Integer.toString(i + 1)).toString();
188                     // Display the default application icon if there is no icon specified by SET-UP
189                     // MENU command nor preset.
190                     Bitmap icon = mBitMap;
191                     if (menu.titleIcon != null) {
192                         icon = menu.titleIcon;
193                         if (menu.titleIconSelfExplanatory) {
194                             stkItemName = null;
195                         }
196                     }
197                     Item item = new Item(i, stkItemName, icon);
198                     mStkMenuList.add(item);
199                 } else {
200                     CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " does not have main menu.");
201                 }
202             } else {
203                 CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is not inserted.");
204             }
205         }
206         if (mStkMenuList != null && mStkMenuList.size() > 0) {
207             if (mStkMenuList.size() > 1) {
208                 StkMenuAdapter adapter = new StkMenuAdapter(this,
209                         mStkMenuList, false);
210                 // Bind menu list to the new adapter.
211                 this.setListAdapter(adapter);
212             }
213             return mStkMenuList.size();
214         } else {
215             CatLog.d(LOG_TAG, "No stk menu item add.");
216             return 0;
217         }
218     }
launchSTKMainMenu(int slodId)219     private void launchSTKMainMenu(int slodId) {
220         Bundle args = new Bundle();
221         CatLog.d(LOG_TAG, "launchSTKMainMenu.");
222         args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
223         args.putInt(StkAppService.SLOT_ID, slodId);
224         startService(new Intent(this, StkAppService.class)
225                 .putExtras(args));
226     }
227 }
228