1/*
2 * Copyright (C) 2014 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.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Bundle;
23import android.view.View;
24
25import com.android.internal.telephony.cat.CatLog;
26import com.android.internal.telephony.PhoneConstants;
27
28import android.telephony.TelephonyManager;
29
30import android.view.Gravity;
31import android.widget.Toast;
32
33/**
34 * Launcher class. Serve as the app's MAIN activity, send an intent to the
35 * StkAppService and finish.
36 *
37 */
38 public class StkMain extends Activity {
39    private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
40    private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
41    private int mSingleSimId = -1;
42    private Context mContext = null;
43    private TelephonyManager mTm = null;
44    private static final String PACKAGE_NAME = "com.android.stk";
45    private static final String STK_LAUNCHER_ACTIVITY_NAME = PACKAGE_NAME + ".StkLauncherActivity";
46
47    @Override
48    public void onCreate(Bundle icicle) {
49        super.onCreate(icicle);
50        CatLog.d(LOG_TAG, "onCreate+");
51        mContext = getBaseContext();
52        mTm = (TelephonyManager) mContext.getSystemService(
53                Context.TELEPHONY_SERVICE);
54        //Check if needs to show the meun list.
55        if (isShowSTKListMenu()) {
56            Intent newIntent = new Intent(Intent.ACTION_VIEW);
57            newIntent.setClassName(PACKAGE_NAME, STK_LAUNCHER_ACTIVITY_NAME);
58            startActivity(newIntent);
59        } else {
60            //launch stk menu activity for the SIM.
61            if (mSingleSimId < 0) {
62                showTextToast(mContext, R.string.no_sim_card_inserted);
63            } else {
64                launchSTKMainMenu(mSingleSimId);
65            }
66        }
67        finish();
68    }
69
70    private boolean isShowSTKListMenu() {
71        int simCount = TelephonyManager.from(mContext).getSimCount();
72        int simInsertedCount = 0;
73        int insertedSlotId = -1;
74
75        CatLog.d(LOG_TAG, "simCount: " + simCount);
76        for (int i = 0; i < simCount; i++) {
77            //Check if the card is inserted.
78            if (mTm.hasIccCard(i)) {
79                CatLog.d(LOG_TAG, "SIM " + i + " is inserted.");
80                mSingleSimId = i;
81                simInsertedCount++;
82            } else {
83                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
84            }
85        }
86        if (simInsertedCount > 1) {
87            return true;
88        } else {
89            //No card or only one card.
90            CatLog.d(LOG_TAG, "do not show stk list menu.");
91            return false;
92        }
93    }
94
95    private void launchSTKMainMenu(int slotId) {
96        Bundle args = new Bundle();
97        CatLog.d(LOG_TAG, "launchSTKMainMenu.");
98        args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
99        args.putInt(StkAppService.SLOT_ID
100                , PhoneConstants.SIM_ID_1 + slotId);
101        startService(new Intent(this, StkAppService.class)
102                .putExtras(args));
103    }
104
105    private void showTextToast(Context context, int resId) {
106        Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
107        toast.setGravity(Gravity.BOTTOM, 0, 0);
108        toast.show();
109    }
110}
111