13483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng/*
23483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Copyright (C) 2010 The Android Open Source Project
33483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
43483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
53483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * you may not use this file except in compliance with the License.
63483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * You may obtain a copy of the License at
73483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
83483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
93483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
103483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Unless required by applicable law or agreed to in writing, software
113483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
123483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * See the License for the specific language governing permissions and
143483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * limitations under the License.
153483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng */
163483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
173483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengpackage com.android.contacts;
183483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
193483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.app.Activity;
203483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.app.AlertDialog;
213483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.app.Dialog;
223483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.app.DialogFragment;
233483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.DialogInterface;
243483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.DialogInterface.OnClickListener;
253483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.Intent;
263483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.net.Uri;
273483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.os.Bundle;
283483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.provider.ContactsContract.Contacts;
293483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.provider.ContactsContract.Intents.Insert;
300319222b43927d4d9ce7e2a9070f3543661b5782Tyler Gunnimport android.telecom.PhoneAccount;
313483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.text.TextUtils;
323483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
333483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng/**
343483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Activity that intercepts DIAL and VIEW intents for phone numbers for devices that can not
353483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * be used as a phone. This allows the user to see the phone number
363483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng */
373483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengpublic class NonPhoneActivity extends ContactsActivity {
383483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
393483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
403483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
413483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    @Override
423483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    protected void onCreate(Bundle savedInstanceState) {
433483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        super.onCreate(savedInstanceState);
443483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        final String phoneNumber = getPhoneNumber();
453483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        if (TextUtils.isEmpty(phoneNumber)) {
463483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            finish();
473483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            return;
483483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        }
493483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
503483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
513483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        Bundle bundle = new Bundle();
523483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        bundle.putString(PHONE_NUMBER_KEY, phoneNumber);
533483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        fragment.setArguments(bundle);
543483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
553483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    }
563483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
573483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    private String getPhoneNumber() {
583483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        if (getIntent() == null) return null;
593483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        final Uri data = getIntent().getData();
603483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        if (data == null) return null;
613483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        final String scheme = data.getScheme();
621cd88e3ecfa72f43c3fe25c912d9f67848f11e60Jay Shrauner        if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
633483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        return getIntent().getData().getSchemeSpecificPart();
643483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    }
653483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
663483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    public static final class NonPhoneDialogFragment extends DialogFragment
673483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            implements OnClickListener {
683483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        @Override
693483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        public Dialog onCreateDialog(Bundle savedInstanceState) {
703483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            final AlertDialog alertDialog;
713483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
723483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    .create();
733483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            alertDialog.setTitle(R.string.non_phone_caption);
743483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            alertDialog.setMessage(getArgumentPhoneNumber());
753483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
763483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    getActivity().getString(R.string.non_phone_add_to_contacts), this);
773483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
783483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    getActivity().getString(R.string.non_phone_close), this);
793483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            return alertDialog;
803483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        }
813483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
823483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        @Override
833483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        public void onClick(DialogInterface dialog, int which) {
843483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            if (which == DialogInterface.BUTTON_POSITIVE) {
853483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
863483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                intent.setType(Contacts.CONTENT_ITEM_TYPE);
873483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
883483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                startActivity(intent);
893483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            }
903483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            dismiss();
913483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        }
923483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
933483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        private String getArgumentPhoneNumber() {
943483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            return getArguments().getString(PHONE_NUMBER_KEY);
953483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        }
963483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
973483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        @Override
983483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        public void onDismiss(DialogInterface dialog) {
993483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            super.onDismiss(dialog);
1003483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            // During screen rotation, getActivity returns null. In this case we do not
1013483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            // want to close the Activity anyway
1023483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            final Activity activity = getActivity();
1033483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            if (activity != null) activity.finish();
1043483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        }
1053483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    }
1063483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng}
107