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; |
| 30 | import android.provider.Settings; |
| 31 | |
| 32 | import com.android.internal.R; |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 33 | import com.android.internal.app.chooser.DisplayResolveInfo; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 39 | * Shows a dialog with actions to take on a chooser target. |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 40 | */ |
| 41 | public class ResolverTargetActionsDialogFragment extends DialogFragment |
| 42 | implements DialogInterface.OnClickListener { |
| 43 | private static final String NAME_KEY = "componentName"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 44 | private static final String TITLE_KEY = "title"; |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 45 | private static final String PINNED_KEY = "pinned"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 46 | |
| 47 | // Sync with R.array.resolver_target_actions_* resources |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 48 | private static final int TOGGLE_PIN_INDEX = 0; |
| 49 | private static final int APP_INFO_INDEX = 1; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 50 | |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 51 | private List<DisplayResolveInfo> mTargetInfos = new ArrayList<>(); |
| 52 | private List<CharSequence> mLabels = new ArrayList<>(); |
| 53 | private boolean[] mPinned; |
| 54 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 55 | public ResolverTargetActionsDialogFragment() { |
| 56 | } |
| 57 | |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 58 | public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name, |
| 59 | boolean pinned) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 60 | Bundle args = new Bundle(); |
| 61 | args.putCharSequence(TITLE_KEY, title); |
| 62 | args.putParcelable(NAME_KEY, name); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 63 | args.putBoolean(PINNED_KEY, pinned); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 64 | setArguments(args); |
| 65 | } |
| 66 | |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 67 | public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name, |
| 68 | List<DisplayResolveInfo> targets, List<CharSequence> labels) { |
| 69 | Bundle args = new Bundle(); |
| 70 | args.putCharSequence(TITLE_KEY, title); |
| 71 | args.putParcelable(NAME_KEY, name); |
| 72 | mTargetInfos = targets; |
| 73 | mLabels = labels; |
| 74 | setArguments(args); |
| 75 | } |
| 76 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 77 | @Override |
| 78 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 79 | final Bundle args = getArguments(); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 80 | final int itemRes = args.getBoolean(PINNED_KEY, false) |
| 81 | ? R.array.resolver_target_actions_unpin |
| 82 | : R.array.resolver_target_actions_pin; |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 83 | String[] defaultActions = getResources().getStringArray(itemRes); |
| 84 | CharSequence[] items; |
| 85 | |
| 86 | if (mTargetInfos == null || mTargetInfos.size() < 2) { |
| 87 | items = defaultActions; |
| 88 | } else { |
| 89 | // Pin item for each sub-item |
| 90 | items = new CharSequence[mTargetInfos.size() + 1]; |
| 91 | for (int i = 0; i < mTargetInfos.size(); i++) { |
| 92 | items[i] = mTargetInfos.get(i).isPinned() |
| 93 | ? getResources().getString(R.string.unpin_specific_target, mLabels.get(i)) |
| 94 | : getResources().getString(R.string.pin_specific_target, mLabels.get(i)); |
| 95 | } |
| 96 | // "App info" |
| 97 | items[mTargetInfos.size()] = defaultActions[1]; |
| 98 | } |
| 99 | |
| 100 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 101 | return new Builder(getContext()) |
| 102 | .setCancelable(true) |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 103 | .setItems(items, this) |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 104 | .setTitle(args.getCharSequence(TITLE_KEY)) |
| 105 | .create(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onClick(DialogInterface dialog, int which) { |
| 110 | final Bundle args = getArguments(); |
| 111 | ComponentName name = args.getParcelable(NAME_KEY); |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 112 | if (which == 0 || (mTargetInfos.size() > 0 && which < mTargetInfos.size())) { |
| 113 | if (mTargetInfos == null || mTargetInfos.size() == 0) { |
| 114 | pinComponent(name); |
| 115 | } else { |
| 116 | pinComponent(mTargetInfos.get(which).getResolvedComponentName()); |
| 117 | } |
| 118 | // Force the chooser to requery and resort things |
Alison Cichowlas | bc29081 | 2019-12-17 19:42:29 -0500 | [diff] [blame] | 119 | ((ChooserActivity) getActivity()).handlePackagesChanged(); |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 120 | } else { |
| 121 | // Last item in dialog is App Info |
| 122 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
| 123 | .setData(Uri.fromParts("package", name.getPackageName(), null)) |
| 124 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 125 | startActivity(in); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 126 | } |
| 127 | dismiss(); |
| 128 | } |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 129 | |
| 130 | private void pinComponent(ComponentName name) { |
| 131 | SharedPreferences sp = ChooserActivity.getPinnedSharedPrefs(getContext()); |
| 132 | final String key = name.flattenToString(); |
| 133 | boolean currentVal = sp.getBoolean(name.flattenToString(), false); |
| 134 | if (currentVal) { |
| 135 | sp.edit().remove(key).apply(); |
| 136 | } else { |
| 137 | sp.edit().putBoolean(key, true).apply(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onConfigurationChanged(Configuration newConfig) { |
| 143 | // Dismiss on config changed (eg: rotation) |
| 144 | // TODO: Maintain state on config change |
| 145 | super.onConfigurationChanged(newConfig); |
| 146 | dismiss(); |
| 147 | } |
| 148 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 149 | } |