Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package com.android.internal.app; |
| 19 | |
| 20 | import android.app.AlertDialog.Builder; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 28 | import android.net.Uri; |
| 29 | import android.os.Bundle; |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 30 | import android.os.UserHandle; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 31 | import android.provider.Settings; |
| 32 | |
| 33 | import com.android.internal.R; |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 34 | import com.android.internal.app.chooser.DisplayResolveInfo; |
| 35 | |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 40 | * Shows a dialog with actions to take on a chooser target. |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 41 | */ |
| 42 | public class ResolverTargetActionsDialogFragment extends DialogFragment |
| 43 | implements DialogInterface.OnClickListener { |
| 44 | private static final String NAME_KEY = "componentName"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 45 | private static final String TITLE_KEY = "title"; |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 46 | private static final String PINNED_KEY = "pinned"; |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 47 | private static final String USER_ID_KEY = "userId"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 48 | |
| 49 | // Sync with R.array.resolver_target_actions_* resources |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 50 | private static final int TOGGLE_PIN_INDEX = 0; |
| 51 | private static final int APP_INFO_INDEX = 1; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 52 | |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 53 | private List<DisplayResolveInfo> mTargetInfos = new ArrayList<>(); |
| 54 | private List<CharSequence> mLabels = new ArrayList<>(); |
| 55 | private boolean[] mPinned; |
| 56 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 57 | public ResolverTargetActionsDialogFragment() { |
| 58 | } |
| 59 | |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 60 | public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name, |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 61 | boolean pinned, UserHandle userHandle) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 62 | Bundle args = new Bundle(); |
| 63 | args.putCharSequence(TITLE_KEY, title); |
| 64 | args.putParcelable(NAME_KEY, name); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 65 | args.putBoolean(PINNED_KEY, pinned); |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 66 | args.putParcelable(USER_ID_KEY, userHandle); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 67 | setArguments(args); |
| 68 | } |
| 69 | |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 70 | public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name, |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 71 | List<DisplayResolveInfo> targets, List<CharSequence> labels, UserHandle userHandle) { |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 72 | Bundle args = new Bundle(); |
| 73 | args.putCharSequence(TITLE_KEY, title); |
| 74 | args.putParcelable(NAME_KEY, name); |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 75 | args.putParcelable(USER_ID_KEY, userHandle); |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 76 | mTargetInfos = targets; |
| 77 | mLabels = labels; |
| 78 | setArguments(args); |
| 79 | } |
| 80 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 81 | @Override |
| 82 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 83 | final Bundle args = getArguments(); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 84 | final int itemRes = args.getBoolean(PINNED_KEY, false) |
| 85 | ? R.array.resolver_target_actions_unpin |
| 86 | : R.array.resolver_target_actions_pin; |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 87 | 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 Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 105 | return new Builder(getContext()) |
| 106 | .setCancelable(true) |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 107 | .setItems(items, this) |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 108 | .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 Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 116 | 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 Cichowlas | bc29081 | 2019-12-17 19:42:29 -0500 | [diff] [blame] | 123 | ((ChooserActivity) getActivity()).handlePackagesChanged(); |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 124 | } 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); |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 129 | UserHandle userHandle = args.getParcelable(USER_ID_KEY); |
| 130 | getActivity().startActivityAsUser(in, userHandle); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 131 | } |
| 132 | dismiss(); |
| 133 | } |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 134 | |
| 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 Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 154 | } |