1/*
2 * Copyright (C) 2015 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 */
16package com.android.messaging.ui.conversation;
17
18import android.app.AlertDialog;
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.os.Bundle;
24import android.text.TextUtils;
25import android.view.LayoutInflater;
26import android.widget.EditText;
27
28import com.android.messaging.R;
29import com.android.messaging.datamodel.ParticipantRefresh;
30import com.android.messaging.util.BuglePrefs;
31import com.android.messaging.util.UiUtils;
32
33/**
34 * The dialog for the user to enter the phone number of their sim.
35 */
36public class EnterSelfPhoneNumberDialog extends DialogFragment {
37    private EditText mEditText;
38    private int mSubId;
39
40    public static EnterSelfPhoneNumberDialog newInstance(final int subId) {
41        final EnterSelfPhoneNumberDialog dialog = new EnterSelfPhoneNumberDialog();
42        dialog.mSubId = subId;
43        return dialog;
44    }
45
46    @Override
47    public Dialog onCreateDialog(final Bundle savedInstanceState) {
48        final Context context = getActivity();
49        final LayoutInflater inflater = LayoutInflater.from(context);
50        mEditText = (EditText) inflater.inflate(R.layout.enter_phone_number_view, null, false);
51
52        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
53        builder.setTitle(R.string.enter_phone_number_title)
54                .setMessage(R.string.enter_phone_number_text)
55                .setView(mEditText)
56                .setNegativeButton(android.R.string.cancel,
57                        new DialogInterface.OnClickListener() {
58                            @Override
59                            public void onClick(final DialogInterface dialog,
60                                    final int button) {
61                                dismiss();
62                            }
63                })
64                .setPositiveButton(android.R.string.ok,
65                        new DialogInterface.OnClickListener() {
66                            @Override
67                            public void onClick(final DialogInterface dialog,
68                                    final int button) {
69                                final String newNumber = mEditText.getText().toString();
70                                dismiss();
71                                if (!TextUtils.isEmpty(newNumber)) {
72                                    savePhoneNumberInPrefs(newNumber);
73                                    // TODO: Remove this toast and just auto-send
74                                    // the message instead
75                                    UiUtils.showToast(
76                                            R.string
77                                        .toast_after_setting_default_sms_app_for_message_send);
78                                }
79                            }
80                });
81        return builder.create();
82    }
83
84    private void savePhoneNumberInPrefs(final String newPhoneNumber) {
85        final BuglePrefs subPrefs = BuglePrefs.getSubscriptionPrefs(mSubId);
86        subPrefs.putString(getString(R.string.mms_phone_number_pref_key),
87                newPhoneNumber);
88        // Update the self participants so the new phone number will be reflected
89        // everywhere in the UI.
90        ParticipantRefresh.refreshSelfParticipants();
91    }
92}
93