ImsFrameworkApp.java revision 02ffa5a99c8d4faf90cbb7639cf0cf41f9f7121d
1/*
2 * Copyright (C) 2011 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.example.imsframework;
18
19import android.app.Application;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.os.AsyncResult;
25import android.os.Handler;
26import android.os.Message;
27import android.telephony.ServiceState;
28import android.telephony.TelephonyManager;
29import android.util.Log;
30
31import com.android.internal.telephony.IccCard;
32import com.android.internal.telephony.Phone;
33import com.android.internal.telephony.PhoneFactory;
34import com.android.internal.telephony.TelephonyIntents;
35
36import java.util.Arrays;
37
38/**
39 * Top-level Application class for the example IMS framework.
40 */
41public class ImsFrameworkApp extends Application {
42    private static final String TAG = "ImsFrameworkApp";
43
44    // Broadcast receiver for telephony intent broadcasts
45    private final BroadcastReceiver mReceiver = new ImsFrameworkBroadcastReceiver();
46
47    // Handler for ISIM authentication callback
48    private final IsimAuthenticationHandler mHandler = new IsimAuthenticationHandler();
49
50    private static final int EVENT_ISIM_AUTHENTICATION_DONE = 100;
51
52    @Override
53    public void onCreate() {
54        // Register for telephony intent broadcasts
55        Log.d(TAG, "onCreate(): registering for telephony state change broadcasts");
56        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
57        intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
58        intentFilter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
59        intentFilter.addAction(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
60        intentFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
61        registerReceiver(mReceiver, intentFilter);
62    }
63
64    /**
65     * Receiver for telephony broadcasts that the IMS framework cares about.
66     */
67    private class ImsFrameworkBroadcastReceiver extends BroadcastReceiver {
68        @Override
69        public void onReceive(Context context, Intent intent) {
70            String action = intent.getAction();
71            Log.d(TAG, "mReceiver received action " + action);
72            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
73                Log.d(TAG, "mReceiver: ACTION_AIRPLANE_MODE_CHANGED");
74                Log.d(TAG, "- state: " + intent.getBooleanExtra(Phone.STATE_KEY, false));
75            } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
76                Log.d(TAG, "mReceiver: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED");
77                Log.d(TAG, "- apnName: " + intent.getStringExtra(Phone.DATA_APN_KEY));
78                Log.d(TAG, "- apnType: " + intent.getStringExtra(Phone.DATA_APN_TYPE_KEY));
79                Log.d(TAG, "- state: " + intent.getStringExtra(Phone.STATE_KEY));
80                Log.d(TAG, "- reason: " + intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY));
81                Log.d(TAG, "- network unavailable: " +
82                        intent.getBooleanExtra(Phone.NETWORK_UNAVAILABLE_KEY, false));
83                // demonstrate calling ISIM authentication when data state changes
84                tryIsimAuthentication();
85            } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
86                Log.d(TAG, "mReceiver: ACTION_SIM_STATE_CHANGED");
87                Log.d(TAG, "- phoneName: " + intent.getStringExtra(Phone.PHONE_NAME_KEY));
88                String state = intent.getStringExtra(IccCard.INTENT_KEY_ICC_STATE);
89                Log.d(TAG, "- state: " + state);
90                Log.d(TAG, "- reason: " + intent.getStringExtra(Phone.STATE_CHANGE_REASON_KEY));
91                if (IccCard.INTENT_VALUE_ICC_LOADED.equals(state)) {
92                    // all ISIM records are loaded, query them through TelephonyManager
93                    handleSimRecordsLoaded();
94                }
95            } else if (action.equals(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED)) {
96                Log.d(TAG, "mReceiver: ACTION_RADIO_TECHNOLOGY_CHANGED");
97                Log.d(TAG, "- phoneName: " + intent.getStringExtra(Phone.PHONE_NAME_KEY));
98            } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
99                Log.d(TAG, "mReceiver: ACTION_SERVICE_STATE_CHANGED");
100                ServiceState ss = ServiceState.newFromBundle(intent.getExtras());
101                Log.d(TAG, "- ServiceState: " + ss);
102            }
103        }
104    }
105
106    void handleSimRecordsLoaded() {
107        TelephonyManager telephonyManager =
108                (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
109
110        Log.d(TAG, "ISIM IMPI: " + telephonyManager.getIsimImpi());
111        Log.d(TAG, "ISIM Domain: " + telephonyManager.getIsimDomain());
112        Log.d(TAG, "ISIM IMPU: " + Arrays.toString(telephonyManager.getIsimImpu()));
113    }
114
115    private final class IsimAuthenticationHandler extends Handler {
116        @Override
117        public void handleMessage(Message msg) {
118            AsyncResult ar = (AsyncResult) msg.obj;
119            if (ar.exception != null) {
120                Log.d(TAG, "requestIsimAuthentication exception: " + ar.exception);
121            } else {
122                String response = (String) ar.result;
123                Log.d(TAG, "requestIsimAuthentication response: " + response);
124            }
125        }
126    }
127
128    void tryIsimAuthentication() {
129        Message response = mHandler.obtainMessage(EVENT_ISIM_AUTHENTICATION_DONE);
130        // Note: this only works when running inside the phone process
131        Phone phone = PhoneFactory.getDefaultPhone();
132        if (phone != null) {
133            phone.requestIsimAuthentication("DUMMY-BASE64-NONCE", response);
134        }
135    }
136}
137