DefaultAppPickerFragment.java revision 2df5a0b322935de5916b05eb9fb29b9883eae1d4
1/*
2 * Copyright (C) 2017 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.applications.defaultapps;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.app.Fragment;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.os.Bundle;
27import android.text.TextUtils;
28import android.util.Pair;
29
30import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31import com.android.settings.R;
32import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33import com.android.settings.widget.RadioButtonPickerFragment;
34import com.android.settings.widget.RadioButtonPreference;
35import com.android.settingslib.applications.DefaultAppInfo;
36import com.android.settingslib.widget.CandidateInfo;
37import com.android.settingslib.wrapper.PackageManagerWrapper;
38
39/**
40 * A generic app picker fragment that shows a list of app as radio button group.
41 */
42public abstract class DefaultAppPickerFragment extends RadioButtonPickerFragment {
43
44    protected PackageManagerWrapper mPm;
45
46    @Override
47    public void onAttach(Context context) {
48        super.onAttach(context);
49        mPm = new PackageManagerWrapper(context.getPackageManager());
50    }
51
52    @Override
53    public void onRadioButtonClicked(RadioButtonPreference selected) {
54        final String selectedKey = selected.getKey();
55        final CharSequence confirmationMessage = getConfirmationMessage(getCandidate(selectedKey));
56        final Activity activity = getActivity();
57        if (TextUtils.isEmpty(confirmationMessage)) {
58            super.onRadioButtonClicked(selected);
59        } else if (activity != null) {
60            final DialogFragment fragment =
61                    newConfirmationDialogFragment(selectedKey, confirmationMessage);
62            fragment.show(activity.getFragmentManager(), ConfirmationDialogFragment.TAG);
63        }
64    }
65
66    @Override
67    protected void onRadioButtonConfirmed(String selectedKey) {
68        mMetricsFeatureProvider.action(getContext(),
69                MetricsEvent.ACTION_SETTINGS_UPDATE_DEFAULT_APP,
70                selectedKey,
71                Pair.create(MetricsEvent.FIELD_CONTEXT, getMetricsCategory()));
72
73        super.onRadioButtonConfirmed(selectedKey);
74    }
75
76    @Override
77    public void bindPreferenceExtra(RadioButtonPreference pref,
78            String key, CandidateInfo info, String defaultKey, String systemDefaultKey) {
79        if (!(info instanceof DefaultAppInfo)) {
80            return;
81        }
82        if (TextUtils.equals(systemDefaultKey, key)) {
83            pref.setSummary(R.string.system_app);
84        } else if (!TextUtils.isEmpty(((DefaultAppInfo) info).summary)) {
85            pref.setSummary(((DefaultAppInfo) info).summary);
86        }
87    }
88
89    protected ConfirmationDialogFragment newConfirmationDialogFragment(String selectedKey,
90            CharSequence confirmationMessage) {
91        final ConfirmationDialogFragment fragment = new ConfirmationDialogFragment();
92        fragment.init(this, selectedKey, confirmationMessage);
93        return fragment;
94    }
95
96    protected CharSequence getConfirmationMessage(CandidateInfo info) {
97        return null;
98    }
99
100    public static class ConfirmationDialogFragment extends InstrumentedDialogFragment
101            implements DialogInterface.OnClickListener {
102
103        public static final String TAG = "DefaultAppConfirm";
104        public static final String EXTRA_KEY = "extra_key";
105        public static final String EXTRA_MESSAGE = "extra_message";
106
107        private DialogInterface.OnClickListener mCancelListener;
108
109        @Override
110        public int getMetricsCategory() {
111            return MetricsEvent.DEFAULT_APP_PICKER_CONFIRMATION_DIALOG;
112        }
113
114        /**
115         * Initializes the fragment.
116         *
117         * <p>Should be called after it's constructed.
118         */
119        public void init(DefaultAppPickerFragment parent, String key, CharSequence message) {
120            final Bundle argument = new Bundle();
121            argument.putString(EXTRA_KEY, key);
122            argument.putCharSequence(EXTRA_MESSAGE, message);
123            setArguments(argument);
124            setTargetFragment(parent, 0);
125        }
126
127        // TODO: add test case for cancelListener
128        public void setCancelListener(DialogInterface.OnClickListener cancelListener) {
129            this.mCancelListener = cancelListener;
130        }
131
132        @Override
133        public Dialog onCreateDialog(Bundle savedInstanceState) {
134            final Bundle bundle = getArguments();
135            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
136                    .setMessage(bundle.getCharSequence(EXTRA_MESSAGE))
137                    .setPositiveButton(android.R.string.ok, this)
138                    .setNegativeButton(android.R.string.cancel, mCancelListener);
139            return builder.create();
140        }
141
142        @Override
143        public void onClick(DialogInterface dialog, int which) {
144            final Fragment fragment = getTargetFragment();
145            if (fragment instanceof DefaultAppPickerFragment) {
146                final Bundle bundle = getArguments();
147                ((DefaultAppPickerFragment) fragment).onRadioButtonConfirmed(
148                        bundle.getString(EXTRA_KEY));
149            }
150        }
151    }
152}
153