1/*
2 * Copyright (C) 2013 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.settings;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.os.Bundle;
26import android.provider.Telephony.Sms.Intents;
27import android.telephony.TelephonyManager;
28
29import com.android.internal.app.AlertActivity;
30import com.android.internal.app.AlertController;
31import com.android.internal.telephony.SmsApplication;
32import com.android.internal.telephony.SmsApplication.SmsApplicationData;
33import com.android.settings.R;
34
35public final class SmsDefaultDialog extends AlertActivity implements
36        DialogInterface.OnClickListener {
37    private ComponentName mNewDefault;
38    private SmsApplicationData mNewSmsApplicationData;
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43
44        Intent intent = getIntent();
45        String packageName = intent.getStringExtra(Intents.EXTRA_PACKAGE_NAME);
46
47        setResult(RESULT_CANCELED);
48        if (!buildDialog(packageName)) {
49            finish();
50        }
51    }
52
53    @Override
54    public void onClick(DialogInterface dialog, int which) {
55        switch (which) {
56            case BUTTON_POSITIVE:
57                SmsApplication.setDefaultApplication(mNewSmsApplicationData.mPackageName, this);
58                setResult(RESULT_OK);
59                break;
60            case BUTTON_NEGATIVE:
61                break;
62        }
63    }
64
65    private boolean buildDialog(String packageName) {
66        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
67        if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
68            // No phone, no SMS
69            return false;
70        }
71
72        mNewSmsApplicationData = SmsApplication.getSmsApplicationData(packageName, this);
73        if (mNewSmsApplicationData == null) {
74            return false;
75        }
76
77        SmsApplicationData oldSmsApplicationData = null;
78        ComponentName oldSmsComponent = SmsApplication.getDefaultSmsApplication(this, true);
79        if (oldSmsComponent != null) {
80            oldSmsApplicationData =
81                    SmsApplication.getSmsApplicationData(oldSmsComponent.getPackageName(), this);
82            if (oldSmsApplicationData.mPackageName.equals(mNewSmsApplicationData.mPackageName)) {
83                return false;
84            }
85        }
86
87        // Compose dialog; get
88        final AlertController.AlertParams p = mAlertParams;
89        p.mTitle = getString(R.string.sms_change_default_dialog_title);
90        if (oldSmsApplicationData != null) {
91            p.mMessage = getString(R.string.sms_change_default_dialog_text,
92                    mNewSmsApplicationData.mApplicationName,
93                    oldSmsApplicationData.mApplicationName);
94        } else {
95            p.mMessage = getString(R.string.sms_change_default_no_previous_dialog_text,
96                    mNewSmsApplicationData.mApplicationName);
97        }
98        p.mPositiveButtonText = getString(R.string.yes);
99        p.mNegativeButtonText = getString(R.string.no);
100        p.mPositiveButtonListener = this;
101        p.mNegativeButtonListener = this;
102        setupAlert();
103
104        return true;
105    }
106}