blob: 35d9bcdbb033d454f69429287e0792f16b5ea7e3 [file] [log] [blame]
Adam Powell23882512016-01-29 10:21:00 -08001/*
2 * Copyright (C) 2016 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
17
18package com.android.internal.app;
19
20import android.app.AlertDialog.Builder;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.ComponentName;
24import android.content.DialogInterface;
25import android.content.Intent;
Alison Cichowlas1fd47152019-11-14 19:50:55 -050026import android.content.SharedPreferences;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050027import android.content.res.Configuration;
Adam Powell23882512016-01-29 10:21:00 -080028import android.net.Uri;
29import android.os.Bundle;
arangelovebf3c052020-03-17 13:11:37 +000030import android.os.UserHandle;
Adam Powell23882512016-01-29 10:21:00 -080031import android.provider.Settings;
32
33import com.android.internal.R;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050034import com.android.internal.app.chooser.DisplayResolveInfo;
35
36import java.util.ArrayList;
37import java.util.List;
Adam Powell23882512016-01-29 10:21:00 -080038
39/**
Alison Cichowlas19ee2922019-12-16 19:43:12 -050040 * Shows a dialog with actions to take on a chooser target.
Adam Powell23882512016-01-29 10:21:00 -080041 */
42public class ResolverTargetActionsDialogFragment extends DialogFragment
43 implements DialogInterface.OnClickListener {
44 private static final String NAME_KEY = "componentName";
Adam Powell23882512016-01-29 10:21:00 -080045 private static final String TITLE_KEY = "title";
Alison Cichowlas1fd47152019-11-14 19:50:55 -050046 private static final String PINNED_KEY = "pinned";
arangelovebf3c052020-03-17 13:11:37 +000047 private static final String USER_ID_KEY = "userId";
Adam Powell23882512016-01-29 10:21:00 -080048
49 // Sync with R.array.resolver_target_actions_* resources
Alison Cichowlas1fd47152019-11-14 19:50:55 -050050 private static final int TOGGLE_PIN_INDEX = 0;
51 private static final int APP_INFO_INDEX = 1;
Adam Powell23882512016-01-29 10:21:00 -080052
Alison Cichowlas19ee2922019-12-16 19:43:12 -050053 private List<DisplayResolveInfo> mTargetInfos = new ArrayList<>();
54 private List<CharSequence> mLabels = new ArrayList<>();
55 private boolean[] mPinned;
56
Adam Powell23882512016-01-29 10:21:00 -080057 public ResolverTargetActionsDialogFragment() {
58 }
59
Alison Cichowlas1fd47152019-11-14 19:50:55 -050060 public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
arangelovebf3c052020-03-17 13:11:37 +000061 boolean pinned, UserHandle userHandle) {
Adam Powell23882512016-01-29 10:21:00 -080062 Bundle args = new Bundle();
63 args.putCharSequence(TITLE_KEY, title);
64 args.putParcelable(NAME_KEY, name);
Alison Cichowlas1fd47152019-11-14 19:50:55 -050065 args.putBoolean(PINNED_KEY, pinned);
arangelovebf3c052020-03-17 13:11:37 +000066 args.putParcelable(USER_ID_KEY, userHandle);
Adam Powell23882512016-01-29 10:21:00 -080067 setArguments(args);
68 }
69
Alison Cichowlas19ee2922019-12-16 19:43:12 -050070 public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
arangelovebf3c052020-03-17 13:11:37 +000071 List<DisplayResolveInfo> targets, List<CharSequence> labels, UserHandle userHandle) {
Alison Cichowlas19ee2922019-12-16 19:43:12 -050072 Bundle args = new Bundle();
73 args.putCharSequence(TITLE_KEY, title);
74 args.putParcelable(NAME_KEY, name);
arangelovebf3c052020-03-17 13:11:37 +000075 args.putParcelable(USER_ID_KEY, userHandle);
Alison Cichowlas19ee2922019-12-16 19:43:12 -050076 mTargetInfos = targets;
77 mLabels = labels;
78 setArguments(args);
79 }
80
Adam Powell23882512016-01-29 10:21:00 -080081 @Override
82 public Dialog onCreateDialog(Bundle savedInstanceState) {
83 final Bundle args = getArguments();
Alison Cichowlas1fd47152019-11-14 19:50:55 -050084 final int itemRes = args.getBoolean(PINNED_KEY, false)
85 ? R.array.resolver_target_actions_unpin
86 : R.array.resolver_target_actions_pin;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050087 String[] defaultActions = getResources().getStringArray(itemRes);
88 CharSequence[] items;
89
90 if (mTargetInfos == null || mTargetInfos.size() < 2) {
91 items = defaultActions;
92 } else {
93 // Pin item for each sub-item
94 items = new CharSequence[mTargetInfos.size() + 1];
95 for (int i = 0; i < mTargetInfos.size(); i++) {
96 items[i] = mTargetInfos.get(i).isPinned()
97 ? getResources().getString(R.string.unpin_specific_target, mLabels.get(i))
98 : getResources().getString(R.string.pin_specific_target, mLabels.get(i));
99 }
100 // "App info"
101 items[mTargetInfos.size()] = defaultActions[1];
102 }
103
104
Adam Powell23882512016-01-29 10:21:00 -0800105 return new Builder(getContext())
106 .setCancelable(true)
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500107 .setItems(items, this)
Adam Powell23882512016-01-29 10:21:00 -0800108 .setTitle(args.getCharSequence(TITLE_KEY))
109 .create();
110 }
111
112 @Override
113 public void onClick(DialogInterface dialog, int which) {
114 final Bundle args = getArguments();
115 ComponentName name = args.getParcelable(NAME_KEY);
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500116 if (which == 0 || (mTargetInfos.size() > 0 && which < mTargetInfos.size())) {
117 if (mTargetInfos == null || mTargetInfos.size() == 0) {
118 pinComponent(name);
119 } else {
120 pinComponent(mTargetInfos.get(which).getResolvedComponentName());
121 }
122 // Force the chooser to requery and resort things
Alison Cichowlasbc290812019-12-17 19:42:29 -0500123 ((ChooserActivity) getActivity()).handlePackagesChanged();
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500124 } else {
125 // Last item in dialog is App Info
126 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
127 .setData(Uri.fromParts("package", name.getPackageName(), null))
128 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
arangelovebf3c052020-03-17 13:11:37 +0000129 UserHandle userHandle = args.getParcelable(USER_ID_KEY);
130 getActivity().startActivityAsUser(in, userHandle);
Adam Powell23882512016-01-29 10:21:00 -0800131 }
132 dismiss();
133 }
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500134
135 private void pinComponent(ComponentName name) {
136 SharedPreferences sp = ChooserActivity.getPinnedSharedPrefs(getContext());
137 final String key = name.flattenToString();
138 boolean currentVal = sp.getBoolean(name.flattenToString(), false);
139 if (currentVal) {
140 sp.edit().remove(key).apply();
141 } else {
142 sp.edit().putBoolean(key, true).apply();
143 }
144 }
145
146 @Override
147 public void onConfigurationChanged(Configuration newConfig) {
148 // Dismiss on config changed (eg: rotation)
149 // TODO: Maintain state on config change
150 super.onConfigurationChanged(newConfig);
151 dismiss();
152 }
153
Adam Powell23882512016-01-29 10:21:00 -0800154}