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.view.WindowManager;
27 import android.widget.ImageView;
28 import android.widget.ListView;
29 import android.widget.TextView;
30 import android.graphics.Bitmap;
31 import android.graphics.BitmapFactory;
32 
33 import com.android.internal.telephony.cat.Item;
34 import com.android.internal.telephony.cat.Menu;
35 import com.android.internal.telephony.cat.CatLog;
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 = StkLauncherActivity.class.getSimpleName();
50     private ArrayList<Item> mStkMenuList = null;
51     private int mSingleSimId = -1;
52     private Context mContext = null;
53     private TelephonyManager mTm = null;
54     private Bitmap mBitMap = null;
55     private boolean mAcceptUsersInput = true;
56 
57     @Override
onCreate(Bundle icicle)58     public void onCreate(Bundle icicle) {
59         super.onCreate(icicle);
60         getWindow().addSystemFlags(
61                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
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
onNewIntent(Intent intent)80     protected void onNewIntent(Intent intent) {
81         super.onNewIntent(intent);
82     }
83 
84     @Override
onListItemClick(ListView l, View v, int position, long id)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 >= 0 && item.id < simCount) {
99             mAcceptUsersInput = false;
100             launchSTKMainMenu(item.id);
101         }
102     }
103 
104     @Override
onKeyDown(int keyCode, KeyEvent event)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
onResume()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
onPause()138     public void onPause() {
139         super.onPause();
140         CatLog.d(LOG_TAG, "onPause");
141     }
142 
143     @Override
onDestroy()144     public void onDestroy() {
145         super.onDestroy();
146         CatLog.d(LOG_TAG, "onDestroy");
147     }
148 
getSelectedItem(int position)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 
addStkMenuListItems()167     private int addStkMenuListItems() {
168         StkAppService appService = StkAppService.getInstance();
169         if (appService == null) {
170             return 0;
171         }
172 
173         String appName = mContext.getResources().getString(R.string.app_name);
174         String stkItemName = null;
175         int simCount = TelephonyManager.from(mContext).getSimCount();
176         mStkMenuList = new ArrayList<Item>();
177 
178         CatLog.d(LOG_TAG, "simCount: " + simCount);
179         for (int i = 0; i < simCount; i++) {
180             // Check if the card is inserted.
181             if (mTm.hasIccCard(i)) {
182                 Menu menu = appService.getMainMenu(i);
183                 // Check if the card has a main menu.
184                 if (menu != null) {
185                     CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is add to menu.");
186                     mSingleSimId = i;
187                     stkItemName = new StringBuilder(menu.title == null ? appName : menu.title)
188                             .append(" ").append(Integer.toString(i + 1)).toString();
189                     // Display the default application icon if there is no icon specified by SET-UP
190                     // MENU command nor preset.
191                     Bitmap icon = mBitMap;
192                     if (menu.titleIcon != null) {
193                         icon = menu.titleIcon;
194                         if (menu.titleIconSelfExplanatory) {
195                             stkItemName = null;
196                         }
197                     }
198                     Item item = new Item(i, stkItemName, icon);
199                     mStkMenuList.add(item);
200                 } else {
201                     CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " does not have main menu.");
202                 }
203             } else {
204                 CatLog.d(LOG_TAG, "SIM #" + (i + 1) + " is not inserted.");
205             }
206         }
207         if (mStkMenuList != null && mStkMenuList.size() > 0) {
208             if (mStkMenuList.size() > 1) {
209                 StkMenuAdapter adapter = new StkMenuAdapter(this,
210                         mStkMenuList, false);
211                 // Bind menu list to the new adapter.
212                 this.setListAdapter(adapter);
213             }
214             return mStkMenuList.size();
215         } else {
216             CatLog.d(LOG_TAG, "No stk menu item add.");
217             return 0;
218         }
219     }
launchSTKMainMenu(int slodId)220     private void launchSTKMainMenu(int slodId) {
221         Bundle args = new Bundle();
222         CatLog.d(LOG_TAG, "launchSTKMainMenu.");
223         args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
224         args.putInt(StkAppService.SLOT_ID, slodId);
225         startService(new Intent(this, StkAppService.class)
226                 .putExtras(args));
227     }
228 }
229