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